Python Forum

Full Version: Python lists help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I am supposed to Use a single list comprehension to create a list of lists where each sublist consists of a pirate’s name,
and the value of his/her plundered sacks of grain (calculate grain values as in part , but only include
those pirates who like parrots.

prairie_pirates = [
['Tractor Jack', 1000, True],
['Plowshare Pete', 950, False],
['Prairie Lily', 245, True],
['Red River Rosie', 350, True],
['Mad Athabasca McArthur', 842, False],
['Assiniboine Sally', 620, True],
['Thresher Tom', 150, True],
['Cranky Canola Carl', 499, False]
]
As you can tell, the list is pirate name, number of sacks of grains, and if they like parrots.

What I have so far is

names_and_plunder_values = [x[0] for x in prairie_pirates if x[2] is True], [x[1]*42 for x in prairie_pirates if x[2] is True]

but this creates two lists, one with names of pirates and one with the grain value, I am supposed to make them all in one list, how can I go on about doing that? thanks
It looks like you want
[[x[0], x[1]*42] for x in prairie_pirates if x[2] is True]
If so, you were really close.
Yes thats exactly what I needed, thank you so much!