0
Can we predict the order in which it get's printed ?
5 ответов
+ 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.
+ 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
+ 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.
0
squares = {1: 1, 2: 4, 3: "error", 4: 16,}
squares[8] = 64
squares[3] = 9
print(squares)
0
{8: 64, 1: 1, 2: 4, 3: 9, 4: 16}
this was the answer