Firstly, the name of the
To get the second values from the tuple, you must iterate over the data.
list
should not be named list
because list
is a built-in function.To get the second values from the tuple, you must iterate over the data.
data = [ ("Rachel", 19), ("Monica", 18), ("Phoebe", 17), ("Joey", 16), ("Ross", 20), ("Chandler", 21), ] for elements in data: print(elements) # ("Rachel",19) ... for elements in data: print(elements[1]) # print second element for name, age in data: print(age) # but name is not used, so you can assgn it the the thow away variable _ for _, age in data: print(age)Or if you want to create a new list only with the second values of the tuples:
from operator import itemgetter def get(data, index): getter = itemgetter(index) return [getter(values) for values in data] # without itemgetter def get(data, index): return [values[index] for values in data] data = [ ("Rachel", 19), ("Monica", 18), ("Phoebe", 17), ("Joey", 16), ("Ross", 20), ("Chandler", 21), ] ages = get(data, 1)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!