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

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

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().

Swift – Stored values order is completely changed in Dictionary

This is because of the definition of Dictionaries: Dictionary A dictionary stores associations between keys of the same type and values of the same type in an collection with no defined ordering. There is no order, they might come out differently than they were put in. This is comparable to NSSet. Edit: NSDictionary Dictionaries Collect … Read more

No generic implementation of OrderedDictionary?

Implementing a generic OrderedDictionary isn’t terribly difficult, but it’s unnecessarily time consuming and frankly this class is a huge oversight on Microsoft’s part. There are multiple ways of implementing this, but I chose to use a KeyedCollection for my internal storage. I also chose to implement various methods for sorting the way that List<T> does … Read more

Converting dict to OrderedDict

You are creating a dictionary first, then passing that dictionary to an OrderedDict. For Python versions < 3.6 (*), by the time you do that, the ordering is no longer going to be correct. dict is inherently not ordered. Pass in a sequence of tuples instead: ship = [(“NAME”, “Albatross”), (“HP”, 50), (“BLASTERS”, 13), (“THRUSTERS”, … Read more

Rename a dictionary key

For a regular dict, you can use: mydict[k_new] = mydict.pop(k_old) This will move the item to the end of the dict, unless k_new was already existing in which case it will overwrite the value in-place. For a Python 3.7+ dict where you additionally want to preserve the ordering, the simplest is to rebuild an entirely … Read more