How Do You Replace Variables With Their Values?

Rhodri James rhodri at kynesim.co.uk
Thu Jul 11 13:45:29 EDT 2019


On 11/07/2019 05:51, Aldwin Pollefeyt wrote:
> dinner = {'Starters':['Fried Calamari', 'Potted crab'],'Main
> Course':['Fish', 'Meat'], 'Desert':['Cake', 'Banana Split']}
> 
> # Don't ask where I got the dinner from
> 
> for meal in dinner.keys():
>      exec(meal.replace(' ','_') + ' = list(dinner[meal])')
> 
> print(Starters)
> print(Main_Course)
> print(Desert)
> 
> OUTPUT:
> ['Fried Calamari', 'Potted crab']
> ['Fish', 'Meat']
> ['Cake', 'Banana Split']

If you think you need to do this, you are almost certainly wrong. Ew!

-- 
Rhodri James *-* Kynesim Ltd



More information about the Python-list mailing list