laszlopanaflex
laszlopanaflex

Reputation: 1916

dictionary comprehension derived from existing lists

as a part of a bigger exercise, I am trying to construct a dictionary based on inputs from smaller lists, but am struggling with an embedded iteration. suppose i have the following illustrative example:

cities = ['newyork','london','tokyo','paris']
citypairs = [i for i in it.combinations(cities,2)]
airlines = ['delta', 'united']

i want to construct an dictionary of dictionaries, "info", whose keys are the combinations of cities above + each airline (so 12 total keys), and each of those keys contains a "city1" and a "city2" key that is populated using the citypairs list. i am trying something like:

info = {
'{city1}/{city2} {airline}'.format(city1=city1, city2=city2, airline=airline): {
    "city1": city1, "city2": city2
    for city1, city2 in citypairs
    }
    for city1, city2 in citypairs
    for airline in airlines 
   } 

but am receiving an invalid syntax error. just to more clearly illustrate what i am after, if instead of the above attempt, i do:

info = {
'{city1}/{city2} {airline}'.format(city1=city1, city2=city2, airline=airline): {
    "city1": "whatever", "city2": "whatever"
    }
    for city1, city2 in citypairs
    for airline in airlines 
   } 

then this will run and simply create dummy values of 'whatever' for city1 and city2 for each key in "info"

this example might seem silly or overly complicated, but the heart of my question is how can i iterate through a list of tuples to populate city1 and city2 in this example - i am after this because the real-life project i am working on would need this sort of flexibility

Upvotes: 0

Views: 54

Answers (2)

Jonathan Eunice
Jonathan Eunice

Reputation: 22463

info = {
'{city1}/{city2} {airline}'.format(**vars()) : 
    { "city1": city1, "city2": city2 }
    for city1, city2 in citypairs
    for airline in airlines 
   }

yields:

{'london/paris delta': {'city1': 'london', 'city2': 'paris'},
 'london/paris united': {'city1': 'london', 'city2': 'paris'},
 'london/tokyo delta': {'city1': 'london', 'city2': 'tokyo'},
 'london/tokyo united': {'city1': 'london', 'city2': 'tokyo'},
 'newyork/london delta': {'city1': 'newyork', 'city2': 'london'},
 'newyork/london united': {'city1': 'newyork', 'city2': 'london'},
 'newyork/paris delta': {'city1': 'newyork', 'city2': 'paris'},
 'newyork/paris united': {'city1': 'newyork', 'city2': 'paris'},
 'newyork/tokyo delta': {'city1': 'newyork', 'city2': 'tokyo'},
 'newyork/tokyo united': {'city1': 'newyork', 'city2': 'tokyo'},
 'tokyo/paris delta': {'city1': 'tokyo', 'city2': 'paris'},
 'tokyo/paris united': {'city1': 'tokyo', 'city2': 'paris'}}

Here I've used a terser form of format() that looks at local variables to find content for the format strings.

You were very close to what you seemed to be asking for. Not sure why you filed in the values for your second-level dicts with "whatever", since you had already generated the city1 and city2 variables in the comprehension. Just using those completes the circuit.

Upvotes: 1

cdlane
cdlane

Reputation: 41872

My belief is that the solution is simpler than you're making it:

from itertools import combinations

cities = ['newyork','london','tokyo','paris']
citypairs = combinations(cities, 2)
airlines = ['delta', 'united']

info = {'{city1}/{city2} {airline}'.format(city1=city1, city2=city2, airline=airline): {"city1": city1, "city2": city2}
    for city1, city2 in citypairs
        for airline in airlines 
} 

print(info)
print()
print(info["newyork/london delta"]["city1"])

OUTPUT

{'london/tokyo delta': {'city1': 'london', 'city2': 'tokyo'}, 'newyork/london delta': {'city1': 'newyork', 'city2': 'london'}, 'london/paris delta': {'city1': 'london', 'city2': 'paris'}, 'london/tokyo united': {'city1': 'london', 'city2': 'tokyo'}, 'tokyo/paris united': {'city1': 'tokyo', 'city2': 'paris'}, 'newyork/paris delta': {'city1': 'newyork', 'city2': 'paris'}, 'tokyo/paris delta': {'city1': 'tokyo', 'city2': 'paris'}, 'newyork/paris united': {'city1': 'newyork', 'city2': 'paris'}, 'london/paris united': {'city1': 'london', 'city2': 'paris'}, 'newyork/london united': {'city1': 'newyork', 'city2': 'london'}, 'newyork/tokyo delta': {'city1': 'newyork', 'city2': 'tokyo'}, 'newyork/tokyo united': {'city1': 'newyork', 'city2': 'tokyo'}}

newyork

Upvotes: 1

Related Questions