1

I am using python 2.7. I have 2 long lists:

id=['avc-asd','asd-red'.....]
name=['car','toy',.....]

print len(id) #600
print len(name) #600

my_dict=dict(zip(id,name))
print len(my_dict) #20
print my_dict
#{'avcf-asd':'car','asd-red':'toy'...}

Any idea why truncation happens? :-/


I also tried using izip_longest but i am getting the same result.

    from itertools import izip_longest

        my_dict=dict(izip_longest(id,name))
        print len(my_dict) #20
jxn
  • 7,685
  • 28
  • 90
  • 172
  • Let me know if this is in line with your problem. If so, I will assign this as a dupe of this: http://stackoverflow.com/questions/19603120/python-avoiding-zip-truncation-of-list – idjaw Oct 16 '15 at 23:50
  • Does your `id list` have duplicates? – sam Oct 16 '15 at 23:50
  • id list has no duplicates but name yes – jxn Oct 16 '15 at 23:51
  • Check [here](http://stackoverflow.com/questions/21388216/python-zip-two-lists-together-without-truncation) – sam Oct 16 '15 at 23:52
  • But my 2 lists have the same length though – jxn Oct 16 '15 at 23:54
  • i used `izip_longest` instead of `zip` and no difference – jxn Oct 17 '15 at 00:01
  • 1
    Works for me in Python 2.7.9. Must be a problem with the contents of the lists (e.g. dictionaries cannot have duplicate keys). Can you post something that will reproduce the problem here? – code_dredd Oct 17 '15 at 00:05
  • oh, i think that solved it. i had name first then id. – jxn Oct 17 '15 at 00:07
  • @jxn: I've posted the comment as the answer, with a bit more explanation. Please accept it if you've found it useful :) – code_dredd Oct 17 '15 at 00:13

1 Answers1

3

I've moved my comment to an answer post as the OP has indicated it solved the problem.

Basically, you need to make sure the keys for the dictionaries are unique and also, as you noted, the order in which you zip things matters. Consider the following example code:

>>> ids = ['1', '2', '3']
>>> names = ['a', 'b', 'a']
>>> dict(zip(ids, names))        # works fine; no duplicates in ids
{'1': 'a', '3': 'a', '2': 'b'}
>>> dict(zip(names, ids))        # truncates due to duplicates in names
{'a': '3', 'b': '2'}
code_dredd
  • 5,915
  • 1
  • 25
  • 53