0

My task: I have 2 lists different size(1,2,3,4 and "qwe","asd","zxc" for example). How I can create dictionary with this lists with next condition: if count of keys more than values- dict[key]=None. If count of values more-just inore it. My code:

list1=[1,2,3,4]
list2=["qwe","asd","zxc"]
dictx={}
for x in range(len(list1)):
if x>len(list2):
    dictx[list1[x]]=None
else:
    dictx[list1[x]]=list2[x]

But I catch error: Traceback (most recent call last): File "", line 5, in dictx[list1[x]]=list2[x] IndexError: list index out of range Where I do mistake?Python 3.4 Upd.It's work, but I try to understand where my mistake in this code. Anybody have thought?

Simple runner
  • 445
  • 6
  • 19
  • Does this answer your question? [How can I make a dictionary (dict) from separate lists of keys and values?](https://stackoverflow.com/questions/209840/how-can-i-make-a-dictionary-dict-from-separate-lists-of-keys-and-values) – Andreas Dec 28 '22 at 22:20

2 Answers2

3

Pad the value list with Nones using itertools.chain and itertools.repeat, then zip the lists:

from itertools import chain, repeat

d = dict(zip(list1, chain(list2, repeat(None))))

repeat(None) is an infinite iterator that yields None forever.

chain(list2, repeat(None)) is an iterator that yields items from list2, then yields Nones when list2 runs out.

zip(...) returns a list of pairs of corresponding elements from list1 and the padded list2. zip stops when any input runs out, so the input is as long as list1.

Finally, dict(...) takes the list of key-value pairs and makes a dict with those keys corresponding to those values.

user2357112
  • 260,549
  • 28
  • 431
  • 505
3

You can also use itertools.izip_longest as follows:

import itertools
list1=[1,2,3,4]
list2=["qwe","asd","zxc"]
print ({l1:l2 for l1,l2 in itertools.izip_longest(list1,list2,fillvalue=None)})

EDIT: In python 3:

import itertools
list1=[1,2,3,4]
list2=["qwe","asd","zxc"]
print ({l1:l2 for l1,l2 in itertools.zip_longest(list1,list2,fillvalue=None)})

Output:

{1: 'qwe', 2: 'asd', 3: 'zxc', 4: None}
venpa
  • 4,268
  • 21
  • 23
  • @user3342929: this answer used Python 2: Add parentheses: `print({l1:l2 .. stuff .. })`, and change `izip` to `zip`. – DSM Apr 12 '14 at 13:28
  • @DSM : Thanks for your help...I tested in python 2.7...now, i have modified to compile in python 3 – venpa Apr 12 '14 at 13:31
  • AttributeError: 'module' object has no attribute 'izip_longest'...What I do wrong?)Sorry, don't see about change. – Simple runner Apr 12 '14 at 13:34
  • 1
    @user3342929: as I said, you have to change `izip` to `zip` -- `izip_longest` was renamed `zip_longest` in Python 3. Try `from itertools import zip_longest` and then `dict(zip_longest(list1, list2))`. – DSM Apr 12 '14 at 13:38
  • Thanks, now it work.But why I can't use my algorithm? Where mistake? – Simple runner Apr 12 '14 at 13:42
  • `izip_longest` looks nice at first, but it doesn't do the right thing when the value list is longer than the key list. – user2357112 Apr 12 '14 at 14:23
  • In this case, what will you expect when value is longer than key list? – venpa Apr 12 '14 at 16:49
  • Excess values should simply be ignored. This answer creates a spurious `None` key mapping to the last excess value. – user2357112 Apr 13 '14 at 00:55