Dictionary | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Dictionary

I wrote following code and the output was rearranged i.e. the arrangement of elements was not the same as given by me: dictn = {'1':1,'b':2,'3':3,'4':4} print(dictn) O/p: {'b': 2, '3': 3, '1': 1, '4': 4} Why?

27th Mar 2017, 10:14 AM
suraj jadhav
suraj jadhav - avatar
9 Answers
+ 13
You can use the sorted function, then iterate back through it: dict_a = {"a": 1, "b": 2, "c": 3} print(dict_a) print(sorted(dict_a)) for key in sorted(dict_a): value = dict_a[key] print(key, ":", value) Also, you can search Google for "sorting python dictionaries".
27th Mar 2017, 11:24 AM
David Hutto
David Hutto - avatar
+ 4
Python dictionaries are unordered. Therefore when you go to print them, the elements will not be in the order you see when you look at the code. However, unordered does not mean there is no order. The order will remain the same for the life span of the object. (Think of unordered as meaning it isn't a reliable order.) This is because dictionaries are stored as a hash value, and the hash value never changes for the life span of the object. If you need to print them in order, you could use tuples: (('1', 1), ('b', 2), ('3', 3),('4', 4))
28th Mar 2017, 3:06 AM
Martian1oh1
Martian1oh1 - avatar
+ 3
@ James: You see a pattern, where there is none. In primes[primes[4]] the innermost expression is evaluated first. primes[4]=7, hence primes [primes[4]] = primes[7]=17. Imagine dictionaries to be functions you define pointwise on a limited domain. They are not ordered, Martian's post explains what unordered means here.
31st Mar 2017, 10:40 PM
Tob
Tob - avatar
+ 2
@Tobi OOOOOOOOH! Why is this so hard? okay okay. so basically it's using a function's answer to recall another entry BASED on the first answer.
31st Mar 2017, 11:19 PM
James Bradley
James Bradley - avatar
+ 1
In dictionaries, data is stored as a key|value pair. However, it is always sorted in a specific order, so key "5" will be stored after key "1", "c" will be stored after "b", etc. You can't affect which key will be stored in which position
27th Mar 2017, 10:40 AM
Anselm
+ 1
data is not sorted, '3' is followed by '1' which is followed by '4'
27th Mar 2017, 10:42 AM
suraj jadhav
suraj jadhav - avatar
+ 1
hi Dwight, i m not trying to sort the 'dictn' but trying to understand why the compiler has changed its order of elements while printing
27th Mar 2017, 11:36 AM
suraj jadhav
suraj jadhav - avatar
+ 1
To piggyback off of this question: Lists are ordered 0 thru Whatever. How how dictionaries ordered? >>>primes = {1: 2, 2: 3, 4: 7, 7:17} print(primes[primes[4]]) prints 17, apparently. So are dictionaries started at number 1 instead of 0 like a list?
31st Mar 2017, 9:54 PM
James Bradley
James Bradley - avatar
0
dictionary is different from list, it is unordered. Think it as a bag carrying a number of paired things. Everytime, you pick something from the bag, it is random. List is more like a queue, things have their own orders.
10th Apr 2017, 1:57 PM
Kai Dai