dictionary comprehension derived from existing lists
<p>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:</p>

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

<p>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: </p>

<pre><code>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 
   } 
</code></pre>

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

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

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

<p>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</p>

<p>My belief is that the solution is simpler than you're making it:</p>

<pre><code>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"])
</code></pre>

<p><strong>OUTPUT</strong></p>

<p>{'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'}}</p>

<p>newyork</p>

python list python-2.7 dictionary