Reputation: 111
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.
Upvotes: 0
Views: 99
Reputation: 7460
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...
Upvotes: 0
Reputation: 18858
print ', '.join(standardpizzalist[x-1] for x in orderedpizzas)
Will give you a comma separated list of ordered pizzas.
Upvotes: 0
Reputation: 353549
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']
Upvotes: 2
Reputation: 2806
Do you mean something like this:
>>> for pizza in orderedpizzas: print standardpizzalist[pizza-1]
1. Supreme
3. Meatlovers
5. BBQ Chicken
Upvotes: 2