Iterating through a tuple of tuples

Asked by At

I have a tuple of tuples and I want to get the second item of its tuple and I am pretty stack on it.Example of what I want:

t=((1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e'),)

and I want to get:

list=['a','b','c','d','e']

2 Answers

1
JohnO On

List comprehension:

my_list = [entry[1] for entry in t]
0
U9-Forward On

Try this using a list comprehension:

print([x for _, x in t])

Another (not really recommended solution):

print(list(map(lambda x: x[1], t)))

Both Output:

['a', 'b', 'c', 'd', 'e']