Right way to initialize an OrderedDict using its constructor such that it retains order of initial data?

The OrderedDict will preserve any order that it has access to. The only way to pass ordered data to it to initialize is to pass a list (or, more generally, an iterable) of key-value pairs, as in your last two examples. As the documentation you linked to says, the OrderedDict does not have access to … Read more

How to get count dict of items but maintain the order in which they appear?

You can use the recipe that uses collections.Counter and collections.OrderedDict: from collections import Counter, OrderedDict class OrderedCounter(Counter, OrderedDict): ‘Counter that remembers the order elements are first encountered’ def __repr__(self): return ‘%s(%r)’ % (self.__class__.__name__, OrderedDict(self)) def __reduce__(self): return self.__class__, (OrderedDict(self),) words = [“oranges”, “apples”, “apples”, “bananas”, “kiwis”, “kiwis”, “apples”] c = OrderedCounter(words) print(c) # OrderedCounter(OrderedDict([(‘oranges’, 1), … Read more

Creating an Ordered Counter

OrderedCounter is given as an example in the OrderedDict documentation, and works without needing to override any methods: class OrderedCounter(Counter, OrderedDict): pass When a class method is called, Python has to find the correct method to execute. There is a defined order in which it searches the class hierarchy called the “method resolution order” or … Read more

OrderedDictionary and Dictionary

You are doing it wrong. You need not only to insert values sequentially into dictionary, but also remove some elements and see how the order has changed after this. The next code demonstrates this: OrderedDictionary od = new OrderedDictionary(); Dictionary<String, String> d = new Dictionary<String, String>(); Random r = new Random(); for (int i = … Read more

Override the {…} notation so i get an OrderedDict() instead of a dict()?

Here’s a hack that almost gives you the syntax you want: class _OrderedDictMaker(object): def __getitem__(self, keys): if not isinstance(keys, tuple): keys = (keys,) assert all(isinstance(key, slice) for key in keys) return OrderedDict([(k.start, k.stop) for k in keys]) ordereddict = _OrderedDictMaker() from nastyhacks import ordereddict menu = ordereddict[ “about” : “about”, “login” : “login”, ‘signup’: “signup” … Read more

Using a list as a data source for DataGridView

First, I don’t understand why you are adding all the keys and values count times, Index is never used. I tried this example : var source = new BindingSource(); List<MyStruct> list = new List<MyStruct> { new MyStruct(“fff”, “b”), new MyStruct(“c”,”d”) }; source.DataSource = list; grid.DataSource = source; and that work pretty well, I get two … Read more

Accessing items in an collections.OrderedDict by index

If its an OrderedDict() you can easily access the elements by indexing by getting the tuples of (key,value) pairs as follows >>> import collections >>> d = collections.OrderedDict() >>> d[‘foo’] = ‘python’ >>> d[‘bar’] = ‘spam’ >>> d.items() [(‘foo’, ‘python’), (‘bar’, ‘spam’)] >>> d.items()[0] (‘foo’, ‘python’) >>> d.items()[1] (‘bar’, ‘spam’) Note for Python 3.X dict.items … Read more

How to sort OrderedDict of OrderedDict?

You’ll have to create a new one since OrderedDict is sorted by insertion order. In your case the code would look like this: foo = OrderedDict(sorted(foo.iteritems(), key=lambda x: x[1][‘depth’])) See http://docs.python.org/dev/library/collections.html#ordereddict-examples-and-recipes for more examples. Note for Python 3 you will need to use .items() instead of .iteritems().