Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Сreate a dictionary from a zip of 3 lists

I have 3 lists that I would like to put into a dictionary:

list1 = ['a', 'b', 'c']
list2 = [1, 2, 3]
list3 = [0.5, 0.3, 0.1]

Traditionally I could create a dictionary like this with just list1, list2

my_dict = dict(zip(list1, list2))
# {'a': 1, 'b': 2, 'c': 3}

But what I would like to get is:

{'a': (1, 0.5), 'b': (2, 0.3), 'c': (3, 0.1)}

This did not work:

my_dict = dict(list1, zip(list2, list3))
like image 295
jxn Avatar asked Aug 10 '15 23:08

jxn


1 Answers

You need to add one more zip, since dict constructor accepts list of tuples, but not two lists:

my_dict_3 = dict(zip(list1, zip(list2, list3)))
like image 66
Yaroslav Admin Avatar answered Oct 04 '22 01:10

Yaroslav Admin