Question: CODING IN PYTHON: List Comprehension Bears 4 Sharks 7 Cats 9 Hedgehogs 6 Ants 2 Aardvarks 11 Output: The three teams with the most wins
CODING IN PYTHON: List Comprehension
Bears 4
Sharks 7
Cats 9
Hedgehogs 6
Ants 2
Aardvarks 11
Output:
The three teams with the most wins are: ['Aardvarks', 'Cats', 'Sharks']
The list i have created from the opening the file below:
[['Bears', '4'], ['Sharks', '7'], ['Cats', '9'], ['Hedgehogs', '6'], ['Ants', '2'], ['Aardvarks', '11']]
file = "teams.txt" file_contents = [line.strip().split(" ") for line in open(file, "r")]
print(file_contents)
Question: Use a list comprehension to create a list of the names of the three teams with the highest wins.
I know I need to sort it by descending but I don't know how to sort by the second element of the list created, which is the number of wins for each time. I'm looking for an answer in List Comprehension.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
