Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python dictionary with list elements as keys and the elements of another list as the values? [duplicate]

So I would like to make a Python dictionary with the elements of one list as the keys and the list elements of another list as the values, is this possible?

So that:

list1 = ('red', 'blue', 'green', many more strings )
list2 = (1, 2, 3, many more values)

d = { list1[0:]: list2[0:] }

This obviously does not work, but something similar?

like image 664
user1882385 Avatar asked Dec 04 '22 00:12

user1882385


1 Answers

You can do it like that:

>>> d = dict(zip(list1, list2))
{'blue': 2, 'green': 3, 'red': 1}
like image 58
dugres Avatar answered Dec 25 '22 11:12

dugres