tegge-classroom / STAT2984-2018

STAT 2984: Statistical Programming I - Spring 2018
1 stars 13 forks source link

How to create a dictionary from two lists? #15

Open mbecker3 opened 6 years ago

mbecker3 commented 6 years ago

I want to create a dictionary from two lists. Online, it says that one way to do it is to simply create a new dictionary and type out all of the keys from both lists. However, depending on the number of keys in the dictionary, this could be a lengthy process. Is there an easier way to do this?

jthough7 commented 6 years ago

Great question!! You need to create 2 lists first. subjects = ('name', 'age', 'food') values = ('Lest Turtles', 42, 'cereal') Then create your dictionary(I found this way online, should not make you have to write it all out) a_dict = {} junk = map(lambda k, v: a_dict.update({k: v}), subjects, values)