1

I have a list of strings of the type cities=['Los Angeles', 'Cancun', 'Paris'], and I want to generate a dict with a key for each item in the list, and an empty array as value, like this:

cities_dict ={
    'Los Angeles': [],
    'Cancun': [],
    'Paris': []
}

I tried this dict(([city.name], []) for city in cities)), but I get a syntax error.

Is there any way I can get this dict without traversing list item by item?

HuLu ViCa
  • 5,077
  • 10
  • 43
  • 93

1 Answers1

1

Recent Python versions have dict comprehensions, so you can do this:

{city: [] for city in cities}
Florian Weimer
  • 32,022
  • 3
  • 48
  • 92