0
import time

sector = 100

dict = {
     0: {"sector1": '', 'time': ''},
     1: {"sector2": '', 'time': ''}
     }

for index in range(2):     
    dict[index].update({'sector'+str(index+1): sector, 'time': time.perf_counter()})
    sector += 100


print(dict)

I have this code above which unexpectedly I saw that dictionary keys and value are being swapped randomly or should I say frequently.

Here is what I mean

{
0: {'sector1': 100, 'time': 80069.410857487}, 
1: {'sector2': 200, 'time': 80069.410862547}
}

The above output is the expected result

{
0: {'time': 79298.567438224, 'sector1': 100}, 
1: {'time': 79298.567442553, 'sector2': 200}
}

but I will get the output above randomly

what is the cause? and does it have any underlying issues if my dict will have a lot of data?

skyline33
  • 573
  • 5
  • 18

1 Answers1

2

Python dicts are unordered, so the internal storage may be in any order. If you would like an ordered dictionary, use collections.OrderedDict

TallChuck
  • 1,725
  • 11
  • 28