'Trying to print the first element of a nested list using list comprehension
I have done my research on the web, even looked at solution for this particular problem from HackerRank but since it is supposed to be an exercise with list comprehension, I keep to it.
I keep getting <generator object <genexpr> at 0x04246760> for the following code:
mainList = []
for _ in range(int(input())):
name = input()
score = float(input())
mainList.append([name,score])
mainList.sort(key = lambda x: [x[1],x[0]])
mainList = list(filter(lambda x: x[1] != mainList[0][1], mainList))
print([y for y,z in x if z == mainList[0][1]] for x in mainList)
print(y for x in mainList for y,z in x if z == mainList[0][1])
Using debug in VSCode, the content of mainList is exactly what I am expecting but the print part errors out.
Both print(...) are my attempt. I just need to know why they don't work.
Here is the input data:
5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39
Solution 1:[1]
mainList = []
for _ in range(int(input())):
name = input()
score = float(input())
mainList.append([name,score])
mainList.sort(key = lambda x: [x[1],x[0]])
mainList = list(filter(lambda x: x[1] != mainList[0][1], mainList))
print(*[x[0] for x in mainList if x[1] == mainList[0][1]], sep = "\n")
Many thanks to Barmar
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jean-Cédric Hamel |
