4

I try to convert a list of tuples to a dictionary but when I do it using the dict() function, the order of the elements is changed:

l = [('id', 2), ('osm_id', 3), ('sourceid', 4), ('notes', 5), ('ref', 6), ('rtenme', 7), ('ntlclass', 8), ('fclass', 9), ('numlanes', 10), ('srftpe', 11), ('srfcond', 12), ('isseasonal', 13), ('curntprac', 14), ('gnralspeed', 15), ('rdwidthm', 16), ('status', 17), ('bridge', 18), ('iso3', 19), ('country', 20), ('last_updat', 21)]

 dict_l = dict(l)
 print (dict_l)

{'fclass': 9, 'status': 17, 'isseasonal': 13, 'bridge': 18, 'sourceid': 4, 'country': 20, 'notes': 5, 'rtenme': 7, 'iso3': 19, 'last_updat': 21, 'gnralspeed': 15, 'osm_id': 3, 'srfcond': 12, 'numlanes': 10, 'srftpe': 11, 'rdwidthm': 16, 'ref': 6, 'id': 2, 'ntlclass': 8, 'curntprac': 14}

What am I missing here?

I also tried to loop through the list of tuples and build the dict manually as:

c = 0
for key, value in display_order_list_sorted:
    display_order_dict_sorted[display_order_list_sorted[c][0]] = display_order_list_sorted[c][1]
    c = c + 1

I faced the same issue.

HelpMePlz
  • 51
  • 7
user1919
  • 3,818
  • 17
  • 62
  • 97
  • 7
    Python dictionaries are unordered. Use an [OrderedDict](https://docs.python.org/3/library/collections.html#collections.OrderedDict). – erip Oct 24 '16 at 14:39
  • 1
    See here for an explanation: [Why is the order in dictionaries and sets arbitrary?](http://stackoverflow.com/questions/15479928/why-is-the-order-in-dictionaries-and-sets-arbitrary) – roganjosh Oct 24 '16 at 14:40
  • 1
    You may be pleased to learn that in Python 3.6 [dictionaries _are_ ordered](http://stackoverflow.com/questions/39980323/dictionaries-are-ordered-in-python-3-6). This is currently an implementation detail & shouldn't be relied on, however, it may eventually become an actual language feature. – PM 2Ring Oct 24 '16 at 14:45
  • These are great news. But we are in 2.7 :) – user1919 Oct 24 '16 at 14:46
  • Python 2 support will cease in 2020, so you better get ready to move to Python 3. :) – PM 2Ring Oct 24 '16 at 14:50

2 Answers2

9
from collections import OrderedDict
l = [('id', 2), ('osm_id', 3), ('sourceid', 4), ('notes', 5), ('ref', 6), ('rtenme', 7), ('ntlclass', 8), ('fclass', 9), ('numlanes', 10), ('srftpe', 11), ('srfcond', 12), ('isseasonal', 13), ('curntprac', 14), ('gnralspeed', 15), ('rdwidthm', 16), ('status', 17), ('bridge', 18), ('iso3', 19), ('country', 20), ('last_updat', 21)]

dict_l = OrderedDict(l)
print (dict_l)
Ari Gold
  • 1,528
  • 11
  • 18
  • So then this answer is misleading: http://stackoverflow.com/questions/6522446/list-of-tuples-to-dictionary – user1919 Oct 24 '16 at 14:42
  • @user1919, how so? – Maciej Gol Oct 24 '16 at 14:43
  • Cause I do the same thing (converting an ordered by value list of tuples to a dictionary) and I don't get an odered by value dictionary as this answer implies. – user1919 Oct 24 '16 at 14:46
  • 2
    It's not misleading, it's just not clear. Dictionary COULD return an ordered dictionary the way you like it, but it's just once in a blue moon if you have a lot of entry. It's "random" basically how it's displayed. – MooingRawr Oct 24 '16 at 14:48
  • As the first entry in the tuple is used as the key, it would be prudent to make sure that the key is not repeated in the original list. Otherwise, some information would be lost. – Ji Wei Jun 05 '20 at 06:08
1

Python dictionary is not ordered. You need to use OrderedDict.

Gulzar rightfully pointed out in the comments, Python dictionaries are now guaranteed to maintain insertion order starting from Python 3.7

Dmitry Yantsen
  • 1,145
  • 11
  • 24