Difference between dictionary and orderdict in python

I am trying to get a sorted dictionary. But the order of the elements between mydict and orddict does not change.

 from collections import OrderedDict mydict = {'a':1,'b':2,'c':3,'d':4} orddict = OrderedDict(mydict) print(mydict,orddict) # print items in mydict: print('mydict') for k,v in mydict.items(): print(k,v) print('ordereddict') # print items in ordered dictionary for k,v in orddict.items(): print(k,v) # print the dictionary keys # for key in mydict.keys(): # print(key) # print the dictionary values # for value in mydict.values(): # print(value) 
+8
source share
4 answers

An OrderedDict saves order items:

 >>> od = OrderedDict() >>> od['c'] = 1 >>> od['b'] = 2 >>> od['a'] = 3 >>> od.items() [('c', 1), ('b', 2), ('a', 3)] >>> d = {} >>> d['c'] = 1 >>> d['b'] = 2 >>> d['a'] = 3 >>> d.items() [('a', 3), ('c', 1), ('b', 2)] 

So, OrderedDict does not order the elements for you, it keeps the order you give it.

If you want to sort the dictionary, you probably want to

 >>> sorted(d.items()) [('a', 1), ('b', 2), ('c', 3)] 
+11
source

Ordered dictionaries are similar to regular dictionaries, but they remember the insertion order of elements. When repeated in an ordered dictionary, items are returned in the order they were added.

Therefore, it only sorts in the order of adding to the dict

You can create an OrderedDict order by key by following below.

orddict = OrderedDict(sorted(mydict.items(), key = lambda t: t[0]))

or just like @ShadowRanger mentioned in the comment

orddict = OrderedDict(sorted(d.items()))

If you want to order by value,

orddict = OrderedDict(sorted(mydict.items(), key = lambda t: t[1]))

Additional information in 8.3.5.1. OrderedDict Examples and Recipes

+3
source

Adding to Brian's answer, OrderedDict really wonderful. That's why:

  • You can use it as a simple dict object because it supports equality testing with other Mapping objects such as collections.counter .

  • OrderedDict preserves the insertion order, as Brian explained. In addition to this, it has a popitem method that returns pairs (key, value) in LIFO order. Thus, you can also use it as a displayed "stack".

You not only get the full dict features, but also some interesting tricks.

+3
source

Starting with Python 3.7 , a new improvement:

the nature of preserving the insertion order of dict objects has been declared an official part of the Python language specification.

This means there is no longer a need for an OrderedDict. They are almost the same. But there are differences that are worth paying attention to.

 from collections import OrderedDict d = {'b': 1, 'a': 2} od = OrderedDict([('b', 1), ('a', 2)]) # they are equal with content and order assert d == od assert list(d.items()) == list(od.items()) assert repr(dict(od)) == repr(d) 

Obviously, there is a difference between the string representation of two objects, with the dict object in a more natural and compact form.

As for the various methods between them, this question can be answered using set theory:

 d_set = set(dir(d)) od_set = set(dir(od)) od_set.difference(d_set) # {'__dict__', '__reversed__', 'move_to_end'} 

This means that OrderedDict has at least two functions that do not have a built-in dict , but workarounds are shown here:

 # 1) OrderedDict can be reversed (but then what?) reversed(od) # <odict_iterator at 0x7fc03f119888> reversed(d) # TypeError: 'dict' object is not reversible # better way to reverse a dict dict(reversed(list(d.items()))) # {'a': 2, 'b': 1} # 2) OrderedDict has 'move_to_end' method od.move_to_end('b') # now it is: OrderedDict([('a', 2), ('b', 1)]) # dict does not, but similar can be done with d['b'] = d.pop('b') # now it is: {'a': 2, 'b': 1} 
+1
source

Source: https://habr.com/ru/post/1238340/


All Articles