ordereddictionary

Sorting a nested OrderedDict by key, recursively

Sorting a nested OrderedDict by key, recursively Question: Say orig is an OrderedDict which contains normal string:string key value pairs, but sometimes the value could be another, nested OrderedDict. I want to sort orig by key, alphabetically (ascending), and do it recursively. Rules: Assume key strings are unpredictable Assume nesting can take place infinitely, e.g. …

Total answers: 6

Changing order of ordered dictionary in python

Changing order of ordered dictionary in python Question: I have an ordered dictionary and want to change the individual order. In the below code example I want to item 3 (people), along with its values, to move to position 2. So the order will be animals, people, food, drinks. How do I go about his? …

Total answers: 3

OrderedDict comprehensions

OrderedDict comprehensions Question: Can I extend syntax in python for dict comprehensions for other dicts, like the OrderedDict in collections module or my own types which inherit from dict? Just rebinding the dict name obviously doesn’t work, the {key: value} comprehension syntax still gives you a plain old dict for comprehensions and literals. >>> from …

Total answers: 3

How to convert an OrderedDict into a regular dict in python3

How to convert an OrderedDict into a regular dict in python3 Question: I am struggling with the following problem: I want to convert an OrderedDict like this: OrderedDict([(‘method’, ‘constant’), (‘data’, ‘1.225’)]) into a regular dict like this: {‘method’: ‘constant’, ‘data’:1.225} because I have to store it as string in a database. After the conversion the …

Total answers: 9

Are there any reasons not to use an OrderedDict?

Are there any reasons not to use an OrderedDict? Question: I’m referring to the OrderedDict from the collections module, which is an ordered dictionary. If it has the added functionality of being orderable, which I realize may often not be necessary but even so, are there any downsides? Is it slower? Is it missing any …

Total answers: 4

Python OrderedDict iteration

Python OrderedDict iteration Question: Why does my python OrderedDict get initialized ‘out of order’? The solution here is less intriguing than the explanation. There’s something here I just don’t get, and perhaps an expanation would help others as well as me. >>> from collections import OrderedDict >>> spam = OrderedDict(s = (1, 2), p = …

Total answers: 3

Python OrderedDict not keeping element order

Python OrderedDict not keeping element order Question: I’m trying to create an OrderedDict object but no sooner do I create it, than the elements are all jumbled. This is what I do: from collections import OrderedDict od = OrderedDict({(0,0):[2],(0,1):[1,9],(0,2):[1,5,9]}) The elements don’t stay in the order I assign od OrderedDict([((0, 1), [1, 9]), ((0, 0), …

Total answers: 1

Converting dict to OrderedDict

Converting dict to OrderedDict Question: I am having some trouble using the collections.OrderedDict class. I am using Python 2.7 on Raspbian, the Debian distro for Raspberry Pi. I am trying to print two dictionaries in order for comparison (side-by-side) for a text-adventure. The order is essential to compare accurately. No matter what I try the …

Total answers: 6

change key in OrderedDict without losing order

change key in OrderedDict without losing order Question: Starting with OrderedDict([(‘a’, 1), (‘c’, 3), (‘b’, 2)]) is it possible to end up with OrderedDict([(‘a’, 1), (‘__C__’, 3), (‘b’, 2)]) making sure that the ‘__C__’ item is before ‘b’ and after ‘a’ i.e. keeping order? Asked By: frnhr || Source Answers: You could try: >>> d …

Total answers: 3