How Do You Replace Variables With Their Values?

Aldwin Pollefeyt aldwinaldwindev at gmail.com
Thu Jul 11 00:51:48 EDT 2019


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']

On Thu, Jul 11, 2019 at 10:34 AM Ben Finney <ben+python at benfinney.id.au>
wrote:

> Terry Reedy <tjreedy at udel.edu> writes:
>
> > On 7/10/2019 6:37 PM, CrazyVideoGamez wrote:
> >
> > > and I'm just changing it with the code above (you can find that by
> > > printing it out). How can I make separate lists called 'Starters',
> > > 'Main Course', and 'Desert'?
> >
> > starters = dinner['Starters']
> > main_course = dinner['Main Course']
> > desert = dinner['Desert']
>
> The question comes, though, why you (CrazyVideoGamez) are doing this.
>
> You have the lists immediately accesible as dictionary elements, by
> name.
>
> Why do you need to also have them bound to separate names; what problem
> are you trying to solve that you think this will help?
>
> --
>  \           “If [a technology company] has confidence in their future |
>   `\      ability to innovate, the importance they place on protecting |
> _o__)     their past innovations really should decline.” —Gary Barnett |
> Ben Finney
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list