Questions tagged [ordereddictionary]

A collection of key-value pairs also accessible by insertion-order.

Links to documentation:

Python 2: collections.OrderedDict

Python 3: collections.OrderedDict

.Net Framework: System.Collections.Specialized.OrderedDictionary

578 questions
694
votes
16 answers

Rename a dictionary key

Is there a way to rename a dictionary key, without reassigning its value to a new name and removing the old name key; and without iterating through dict key/value? In case of OrderedDict do the same, while keeping that key's position.
rabin utam
  • 13,930
  • 3
  • 20
  • 15
463
votes
6 answers

Can I get JSON to load into an OrderedDict?

Ok so I can use an OrderedDict in json.dump. That is, an OrderedDict can be used as an input to JSON. But can it be used as an output? If so how? In my case I'd like to load into an OrderedDict so I can keep the order of the keys in the file. If…
c00kiemonster
  • 22,241
  • 34
  • 95
  • 133
186
votes
9 answers

Accessing items in an collections.OrderedDict by index

Lets say I have the following code: import collections d = collections.OrderedDict() d['foo'] = 'python' d['bar'] = 'spam' Is there a way I can access the items in a numbered manner, like: d(0) #foo's Output d(1) #bar's Output
Billjk
  • 10,387
  • 23
  • 54
  • 73
166
votes
13 answers

No generic implementation of OrderedDictionary?

There doesn't appear to be a generic implementation of OrderedDictionary (which is in the System.Collections.Specialized namespace) in .NET 3.5. Is there one that I'm missing? I've found implementations out there to provide the functionality, but…
AdaTheDev
  • 142,592
  • 28
  • 206
  • 200
160
votes
6 answers

Converting dict to OrderedDict

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…
ninthpower
  • 1,735
  • 2
  • 11
  • 6
147
votes
9 answers

How to convert an OrderedDict into a regular dict in python3

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…
Ben A.
  • 1,511
  • 2
  • 11
  • 8
144
votes
3 answers

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

What's the correct way to initialize an ordered dictionary (OD) so that it retains the order of initial data? from collections import OrderedDict # Obviously wrong because regular dict loses order d = OrderedDict({'b':2, 'a':1}) # An OD is…
click
  • 2,093
  • 2
  • 15
  • 21
118
votes
15 answers

Any way to properly pretty-print OrderedDict?

I like the pprint module in Python. I use it a lot for testing and debugging. I frequently use the width option to make sure the output fits nicely within my terminal window. It has worked fine until they added the new ordered dictionary type in…
Elias Zamaria
  • 96,623
  • 33
  • 114
  • 148
71
votes
4 answers

Are there any reasons not to use an OrderedDict?

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…
temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
69
votes
4 answers

Last element in OrderedDict

I have od of type OrderedDict. I want to access its most recently added (key, value) pair. od.popitem(last = True) would do it, but would also remove the pair from od which I don't want. What's a good way to do that? Can /should I do this: class…
max
  • 49,282
  • 56
  • 208
  • 355
69
votes
1 answer

OrderedDictionary and Dictionary

I was looking for a way to have my Dictionary enumerate its KeyValuePair in the same order that they were added. Now, Dictionary's documentation clearly states that: For purposes of enumeration, each item in the dictionary is treated as a…
DeadlyJesus
  • 1,503
  • 2
  • 12
  • 26
67
votes
3 answers

OrderedDict comprehensions

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…
wim
  • 338,267
  • 99
  • 616
  • 750
65
votes
3 answers

How to sort OrderedDict of OrderedDict?

I'm trying to sort OrderedDict in OrderedDict by 'depth' key. Is there any solution to sort that Dictionary ? OrderedDict([ (2, OrderedDict([ ('depth', 0), ('height', 51), ('width', 51), ('id', 100) ])), (1,…
Damian Gądziak
  • 895
  • 1
  • 9
  • 12
53
votes
3 answers

Why are the values of an OrderedDict not equal?

With Python 3: >>> from collections import OrderedDict >>> d1 = OrderedDict([('foo', 'bar')]) >>> d2 = OrderedDict([('foo', 'bar')]) I wanted to check for equality: >>> d1 == d2 True >>> d1.keys() == d2.keys() True But: >>> d1.values() ==…
Alexandre
  • 1,635
  • 14
  • 21
50
votes
1 answer

Python OrderedDict not keeping element order

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…
Ecolitan
  • 611
  • 1
  • 6
  • 9
1
2 3
38 39