D
D
Dmitry Vyatkin2016-04-04 00:46:20
Python
Dmitry Vyatkin, 2016-04-04 00:46:20

What is the correct way to create a dictionary from two lists using itertools?

There are two lists of different lengths. The first contains the keys, and the second contains the values. Write a function that creates a dictionary from these keys and values. If the key didn't have enough value, the dictionary value should be None. Values ​​that do not have enough keys should be ignored.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2016-04-04
@dim137

from itertools import zip_longest
{k: v for k, v in zip_longest(a, b) if k is not None}

Hello teacher.

B
Bulat Kurbangaliev, 2016-04-04
@ilov3

https://docs.python.org/2/library/itertools.html#i...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question