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
  • 4,188
  • 5
  • 34
  • 65

1 Answers1

1

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

{city: [] for city in cities}
Florian Weimer
  • 29,521
  • 3
  • 37
  • 79