Dictionary keeps the order correctly when there are around 1,2 or 3 elements only
>>> a = ["dorian", "strawberry", "apple"]
>>> b = ["sweet", "delicious", "tasty"]
>>> c = dict(zip(a, b))
>>> c
{'dorian': 'sweet', 'strawberry': 'delicious', 'apple': 'tasty'}
But when there are over 3 elements, the order is broken
>>> a = ["dorian", "strawberry", "apple", "coconut"]
>>> b = ["sweet", "delicious", "tasty", "yum"]
>>> c = dict(zip(a, b))
>>> c
{'strawberry': 'delicious', 'coconut': 'yum', 'dorian': 'sweet', 'apple': 'tasty'}
Could anyone please explain why it happens that way ? thanks
Python dictionaries don’t maintain any order, you should use
OrderedDictfor that.