1

I have a dictionary called wantedPoints.
Performing a deep copy on the dictionary using this line:

copyWantedPoints = deepcopy(wantedPoints)

The result I get is this:

wantedPoints

{'AB12': [], 'GPS1': [], 'GPS3': [], 'BS3': [], 'AB41': [], 'C3': [], 'AB43': [], 'AB42': [], 'AB45': [], 'AB44': [], 'AB47': [], 'AB46': [], 'AB49': [], 'AB48': [], 'C2': [], 'C5': []}  

copyWantedPoints

{'AB12': [], 'GPS1': [], 'GPS3': [], 'BS3': [], 'AB41': [], 'AB49': [], 'AB43': [], 'AB42': [], 'AB45': [], 'AB44': [], 'AB47': [], 'AB46': [], 'C3': [], 'AB48': [], 'C2': [], 'C5': []}

As you can see, everything stays the same except for two keys, C3 and AB49 changed their position in the dictionary.
Any idea why that happens?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Elia
  • 762
  • 1
  • 12
  • 28
  • I know that they are unordered but are you saying that copying a dictionary after initializing it changes it's order again? So copying a dictionary N times would result in N different keys orders? – Elia Dec 22 '15 at 11:40
  • 1
    I am saying that you are creating a new dict so it is possible to come in any order, it could be the same or completely different. – Padraic Cunningham Dec 22 '15 at 11:41
  • @Elia: you have a hash collision between those two keys: `hash('AB49') % 32` and `hash('C3') % 32` are the same. Their order depends on which key is inserted first. There are no other collisions in your dict, except for one other key pair (and the order doesn't change here because the insertion order is "correct"). – Alex Riley Dec 22 '15 at 11:44
  • @ajcr Thank you for the explanation. – Elia Dec 22 '15 at 11:47
  • Try printing `wantedPoints` _before_ you do the deepcopy operation. You will see that the order of the keys is _not_ the same as the order in the literal you used to create the dictionary, and is, in fact, the same as the order shown for `copyWantedPoints`. – PM 2Ring Dec 22 '15 at 11:48
  • @PM2Ring Actually it prints the dictionary in the desired order which is the same as ''wantedPoints'' above. – Elia Dec 22 '15 at 11:49
  • For more info, so Martijn's excellent answer to [Why is the order in Python dictionaries and sets arbitrary?](http://stackoverflow.com/q/15479928/4014959) – PM 2Ring Dec 22 '15 at 11:50
  • Well in order to "bypass" this issue I just worked on the original dictionary and cleared it to its original values after. Not well done but suits my purpose. Will think twice about when to use a dictionary next time. – Elia Dec 22 '15 at 11:56

1 Answers1

1

Dictionaries are not ordered containers, thats why, in case you want an ordered Continer take a look over OrderedDict

Insertion order is not assured using dicts, deepcopy is just looping and copying all the values into a new dict, so the "order" of the keys can be diferen by implementation. Notice that deepcopy does not copy it like a C++ byte copy for example, it just takes all the keys and all the values(copying them too) and put them together.

Take a look at this post of how a python dictionary example is implemented

Netwave
  • 40,134
  • 6
  • 50
  • 93
  • Since i'm copying a dictionary (that must have been ordered by something, not just random) isn't it supposed to be in the same order since I haven't changed anything? Otherwise why only two points have changed their keys position? – Elia Dec 22 '15 at 11:39
  • @Ellia, dicts are not implemented for an ordered behaviour and they don't keep track of an insertion order (or any kind of order). Also, the order is not random as you suggest, it deals with the hashing of the keys. – Netwave Dec 22 '15 at 11:44