0

Sorry if the question is worded awkwardly. What I'm tyring to do:

I have a list.

standardpizzalist = ["1. Supreme", "2. Godfather", "3. Meatlovers", "4. Capricosa", "5. BBQ Chicken", "6. Prawn Delight", "7. Basil and Pork",] 

The customer has this list displayed, they then enter which selections they want, eg. 1, 3, 5 would represent Supreme, Meatlovers and BBQ Chicken. These values are saved in a list also:

orderedpizzas = [1, 3, 5] 

I need the list orderedpizzas to be displayed on screen, but instead of 1, 3 and 5 being printed, I need it to read the values from the other list. eg. 1. Supreme, 3. Meatlovers, 5. BBQ Chicken.

4 Answers 4

2

Do you mean something like this:

>>> for pizza in orderedpizzas: print standardpizzalist[pizza-1] 1. Supreme 3. Meatlovers 5. BBQ Chicken 
Sign up to request clarification or add additional context in comments.

2 Comments

perfect! I think this answers my question exactly! Will vote correct soon.
Awesome! I even simplified it to a one-liner ;-)
2

I think you're better off using a dictionary instead:

>>> pizza_dict {1: 'Supreme', 2: 'Godfather', 3: 'Meatlovers', 4: 'Capricosa', 5: 'BBQ Chicken', 6: 'Prawn Delight', 7: 'Basil and Pork'} >>> pizza_dict[1] 'Supreme' >>> pizza_dict[3] 'Meatlovers' >>> pizza_dict[5] 'BBQ Chicken' 

And then we can use a list comprehension to get the names:

>>> ordered_pizzas = [1,3,5] >>> names = [pizza_dict[num] for num in ordered_pizzas] >>> names ['Supreme', 'Meatlovers', 'BBQ Chicken'] 

Comments

0
print ', '.join(standardpizzalist[x-1] for x in orderedpizzas) 

Will give you a comma separated list of ordered pizzas.

Comments

0

Use an ordered dictionary.

from collections import OrderedDict pizzas = OrderedDict() pizzas[1] = 'Supreme' pizzas[2] = 'Godfather' ...etc... 

So then when you iterate over the items, the ordering is kept(unlike a regular dict):

for index, pizza in pizzas.items(): print pizza # First prints 'Supreme', then 'Godfather', etc... 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.