Use items in a list as new dictionaries

时间:2016-01-16 19:33:13

标签: python list dictionary

I'm trying to iterate through the items in a list:

cities = ['San Francisco', 'Los Angeles', 'Phoenix', 'Las Vegas', 'Dallas', 'Miami', 'Washington DC', 'New York']

in order to create new dictionaries with the city name i.e. Phoenix = {}.

for city in cities:
    city = {}

But instead a dictionary named city is created:

>>> city
{}
>>> type(city)
<class 'dict'>

No dictionaries with the name of the city are created:

>>> Phoenix
Traceback (most recent call last):
  File "<pyshell#122>", line 1, in <module>
    Phoenix
NameError: name 'Phoenix' is not defined

What am I doing wrong?

0 个答案:

没有答案