-1

I was trying to create a dictionary using 2 lists of same length (42 in this case) using zip().

Something like this

wordslist = ['If','you','let','my','daughter','go','now',"that'll",'be','the','end','of','it','I','will','not','look','for','you','I','will','not','pursue','you','But','if','you',"don't",'I','will','look','for','you','I','will','find','you','and','I','will','kill','you']

keylist = [2, 3, 3, 2, 8, 2, 3, 7, 2, 3, 3, 2, 2, 1, 4, 3, 4, 3, 3, 1, 4, 3, 6, 3, 3, 2, 3, 5, 1, 4, 4, 3, 3, 1, 4, 4, 3, 3, 1, 4, 4, 3]

a = dict(zip(wordslist,keylist))
b = dict(zip(keylist,wordslist))

The dictionaries created in case a and case b are of different lengths. a is 25 and b is 8. I am not able to understand why.

pmqs
  • 3,066
  • 2
  • 13
  • 22
  • 1
    Take a simpified example, like `wordslist = ["I", "you", "a", "the", "you"]` and `keylist = [1, 3, 1, 3, 3]`. What would you expect the results for your `a` and `b` to be in that case? – slothrop Feb 12 '23 at 10:01
  • How many unique elements (i.e. keys in resulting dict) you have in `wordlist` and in `keylist`? – buran Feb 12 '23 at 10:07
  • The reason why your *b* dictionary has less content than your *a* dictionary is because dictionaries can't have duplicate keys. The length of *b* will be equal to *len(set(keylist))* In fact, there are duplicates in *wordslist* therefore *len(a) == len(set(wordslist))* – DarkKnight Feb 12 '23 at 10:31
  • Welcome to Stack Overflow. "I am not able to understand why." Did you try to **look at** the resulting dictionaries? Exactly what result did you expect instead, and why? I assume that you expected that there would be 42 keys in each case - yes? But, of course, either way, some of them would be duplicates - right? Do you see any duplicates in the results that you actually get? Does this explain the result? – Karl Knechtel Feb 12 '23 at 12:23
  • Thank you guys. I realised my mistake. I see that keys need to be unique, hence the dictionaries of different lengths, also the length of the dictionary is shorter than both lists. Very new to coding, learning Python from scratch, hence the stupid question. Thank you all . – Godfather_NKP Feb 13 '23 at 13:54

2 Answers2

0

Because you call dict, python coerces your zipped file into a dict as expected. A dict however must have unique keys. When you zip(keylist, wordlist), many of the elements in keylist are duplicated. In this instance, dict merely overwrites previous entries with same key.

Galo do Leste
  • 703
  • 5
  • 13
  • thank you. so unique keys is the answer here I see. i did observe that the dictionary lengths created in case of a and b were 28 and 8 respectively. Thank you for the answer. very new to python, learning from scratch by myself. hence the stupid question. It seems to have been flagged already lol . Thanks anyway! – Godfather_NKP Feb 13 '23 at 13:49
-1

When you are using zip(a, b) you will get zip value such as tuple values like [("a", 1), ("b", 2), ..] by default when you creating dictionaries using dict and tuple the first element of the tupple will be the key and the second will be the value. That's why in case wordslist,keylist the generated keys will be like If, you, etc otherwise in keylist,wordslist the key will be the number.

ramadnsyh
  • 9
  • 1