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

How Do You Replace Variables With Their Values?

On 7/10/2019 6:37 PM, CrazyVideoGamez wrote:

the below twice.  Please post just once and be patient for at least a 
couple of hours -- or days if not subscribed.

> How do you replace a variable with its value in python 3.7.2?

Use the variable (name) in an expression.  But this is not what you ask 

> For example, say I have:
> dinner = {'Starters':['Fried Calamari', 'Potted crab'],'Main Course':['Fish', 'Meat'], 'Desert':['Cake', 'Banana Split']}
> # Don't ask where I got the dinner from

I think most of us have seen examples like this in programming books.

> for meal in dinner.keys():
>          meal = list(dinner[meal]
> But I only get one list called "meal"

Because it is just one identifier, and you rebind it to multiple values.

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

Terry Jan Reedy