osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How Do You Replace Variables With Their Values?


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
>