[Tutor] Creating a dictionary on user filter

Peter Otten __peter__ at web.de
Fri Jul 20 09:19:52 CEST 2012


Mike Nickey wrote:

> What I have is this:
> firstList = ['a', 'b', 'c']
> secondList = [1,2,3]
> thirdList = [1.20, 1.23, 2.54]
> 
> What I am looking for is something like this for output:
> {'a': [1, 1.20], 'b': [2, 1.23], 'c': [3, 2.54]}

To get this combine second and third into the list of values and then build 
the final dict using it:

>>> first = ['a', 'b', 'c']
>>> second = [1, 2, 3]
>>> third = [1.20, 1.23, 2.54]
>>> values = zip(second, third)
>>> values
[(1, 1.2), (2, 1.23), (3, 2.54)]
>>> dict(zip(first, values))
{'a': (1, 1.2), 'c': (3, 2.54), 'b': (2, 1.23)}

If tuples as values are not acceptable: 

values = map(list, values)




More information about the Tutor mailing list