jxn
jxn

Reputation: 8025

С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))

Upvotes: 6

Views: 6464

Answers (1)

Yaroslav Admin
Yaroslav Admin

Reputation: 14535

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)))

Upvotes: 9

Related Questions