Jun-01-2017, 05:29 AM
More of that. The order of the dict in Python 3.6 fallow the order of inserting its elements.
So, if you delete a key and it's value and insert again the same key with a new value they will be positioned after all the others when printing it.
So, if you delete a key and it's value and insert again the same key with a new value they will be positioned after all the others when printing it.
In [1]: dict_ = {num: num**2 for num in range(2,5)} In [2]: dict_ Out[2]: {2: 4, 3: 9, 4: 16} In [3]: dict_.pop(3) Out[3]: 9 In [4]: dict_ Out[4]: {2: 4, 4: 16} In [5]: dict_[3] = 3**3 In [6]: dict_ # here the order is preserved because of the interpreter. It's doing it Out[6]: {2: 4, 3: 27, 4: 16} In [7]: dict_[1] = 'one' In [8]: dict_ Out[8]: {1: 'one', 2: 4, 3: 27, 4: 16} # the same In [9]: print(dict_) # but {2: 4, 4: 16, 3: 27, 1: 'one'}