Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python lists help
#1
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
Reply
#2
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.
Reply
#3
Yes thats exactly what I needed, thank you so much!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020