hugole98
hugole98

Reputation: 11

Selecting part of a string in a list

I'm trying to write a program that displays the names of the planets in the list Planets in descending order by their position from the Sun.

As you can see below, I've managed to figure out how to sort the different planets by their position from the Sun (third value in the strings) but I can't figure out how to remove the numbers and just display the text, as it displays the whole strings from the list and not just the name of the planet which is what I would want ideally. Any ideas?

planet_tuples=[
    ("Mercury",75,1),
    ("Venus",460,2),
    ("Mars",140,4),
    ("Earth",510,3),
    ("Jupiter",62000,5),
    ("Neptune",7640,8),
    ("Saturn",42700,6),
    ("Uranus",8100,7),
    ]
s=(sorted(planet_tuples,key=lambda planet: planet[2],reverse=True))
s.remove(planet_tuples[])
print (s)

Upvotes: 1

Views: 68

Answers (1)

niraj
niraj

Reputation: 18208

You can extract first element of tuple with list comprehension:

print ([x[0] for x in s])

Result:

['Neptune', 'Uranus', 'Saturn', 'Jupiter', 'Mars', 'Earth', 'Venus', 'Mercury']

Upvotes: 1

Related Questions