Suppose I have two lists, and I want to make a dictionary out of them. How:
>>> l = [1, 2, 3, 4, 5]
>>> x = ['a', 'b', 'c']
>>> dict(zip(l, x))
{1: 'a', 2: 'b', 3: 'c'}
It works the way I want to, and because the lists are not of equal length, the elements 4and 5are not considered, and for them there is no corresponding value. This is as expected.
But what if I want a value, say Nonefor the keys in l? I want the result to be as follows:
{1: 'a', 2: 'b', 3: 'c', 4: None, 5: None}
One of the solutions I thought was to iterate over both, compare their lengths and attach Nonewhere necessary. I have a solution that also works, but I was wondering, is it possible to do this much easier and shorter?
source
share