Can we predict the order in which it get's printed ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can we predict the order in which it get's printed ?

15th May 2019, 3:36 PM
Mudamala JAYASIMHA REDDY
Mudamala JAYASIMHA REDDY - avatar
5 Answers
+ 3
This is the output order on my system from print(squares) before and after adding and modifying the dict: {1: 1, 2: 4, 3: 'error', 4: 16} {1: 1, 2: 4, 3: 9, 4: 16, 8: 64} What is your output? For your information: There is an alternativ way to add or update dicts: #squares[8] = 64 #squares[3] = 9 squares.update({8: 64, 3: 9}) This allows to pass more than one key:value pair at the same time.
15th May 2019, 4:41 PM
Lothar
Lothar - avatar
+ 3
Here is a comparison of python versions: V2.7: {1: 1, 2: 4, 3: 'error', 4: 16} {8: 64, 1: 1, 2: 4, 3: 9, 4: 16} V3.6: {1: 1, 2: 4, 3: 'error', 4: 16} {1: 1, 2: 4, 3: 9, 4: 16, 8: 64} If its still an issue for you have a look at collections.OrderedDict() https://stackoverflow.com/questions/15711755/converting-dict-to-ordereddict#15711843
16th May 2019, 5:51 AM
Lothar
Lothar - avatar
+ 2
You are talking about python? Can you give us a sample please? It would also be interesting which version of python you are using.
15th May 2019, 3:43 PM
Lothar
Lothar - avatar
0
squares = {1: 1, 2: 4, 3: "error", 4: 16,} squares[8] = 64 squares[3] = 9 print(squares)
15th May 2019, 3:45 PM
Mudamala JAYASIMHA REDDY
Mudamala JAYASIMHA REDDY - avatar
0
{8: 64, 1: 1, 2: 4, 3: 9, 4: 16} this was the answer
15th May 2019, 4:21 PM
Mudamala JAYASIMHA REDDY
Mudamala JAYASIMHA REDDY - avatar