I have some error in Python 3 while using dictionaries. The input and output does not match

- 6,249
- 6
- 28
- 36

- 1
- 1
-
6Copy paste your code here. An image does not help. – Ma0 May 29 '18 at 09:13
-
2Dictionaries are _not_ ordered – DavidG May 29 '18 at 09:13
-
1dictionaries are not ordered in python, so order will not be preserved. Used OrderedDict instead: https://docs.python.org/2/library/collections.html – Jonas May 29 '18 at 09:13
-
1Generally you can not rely on any ordering in dictionaries, however, if you are using Python >= 3.7 dictionaries maintain insertion order = as you expected. The same is true for _CPython_ implementations >= 3.6. – mhawke May 29 '18 at 09:18
1 Answers
What you are getting is not an error. Read about dictionaries first: https://www.w3schools.com/python/python_dictionaries.asp
Dictionaries don't work as list. They do not have order. They are hashed data structure that strongly binds keys with value. 5
will always be bound with "five"
and 4
will always be bound with "four"
. If you type dict1[5]
, you will always get 'five'
. In dictionaries, order of arrangement is not important, because python uses complex algorithms to keep key - value bound by hashing, and these algorithms may alter the order of arrangement, but order of arrangement is anyways not important for us in dictionaries.
Never use dictionaries as lists. Dictionaries are collection of key value pairs and you access a value by a key. Lists are like arrays, you access a value by index.

- 765
- 13
- 32
-
1This is somewhat out of date. Python >= 3.7 dictionaries maintain insertion order, as do 3.6 CPython implementations. – mhawke May 29 '18 at 09:21
-
I am not sure about 3.7, but still in the context of this question, the order of arrangement in dictionary is supposed to be irrelevant. BTW thanks for updating me with this info about 3.7 :) – harshvardhan May 29 '18 at 09:22
-
thanks for helping me out in this stuff , I am new to python that's why i don't know much about it. – Govind Bhardwaj Jun 01 '18 at 15:51