How to map multiple lists to one dictionary?

dictionary, list, python

Solution

dict((z[0], list(z[1:])) for z in zip(list1, list2, list3))

will work. Or, if you prefer the slightly nicer dictionary comprehension syntax:

{z[0]: list(z[1:]) for z in zip(list1, list2, list3)}

This scales up to to an arbitrary number of lists easily:

list_of_lists = [list1, list2, list3, ...]
{z[0]: list(z[1:]) for z in zip(*list_of_lists)} 

And if you want to convert the type to make sure that the value lists contain all integers:

def to_int(iterable):
    return [int(x) for x in iterable]

{z[0]: to_int(z[1:]) for z in zip(*list_of_lists)}

Of course, you could do that in one line, but I'd rather not.

Problem

I used this code: ``` dictionary = dict(zip(list1, list2)) ``` in order to map two lists in a dictionary. Where: ``` list1 = ('a','b','c') list2 = ('1','2','3') ``` The dictionary equals to: ``` {'a': 1, 'c': 3, 'b': 2} ``` Is there a way to add a third list: ``` list3 = ('4','5','6') ``` so that the dictionary will equal to: ``` {'a': [1,4], 'c': [3,5], 'b': [2,6]} ``` This third list has to be added so that it follows the existing mapping. The idea is to make this work iteratively in a for loop and several dozens of values to the correctly mapped keyword. Is something like this possible?

Original source