I have a list of sublists, such as:
[[501, 4], [501, 4], [501, 4], [501, 4]]
How can I get rid of the second element for each sublist ? (i.e. 4)
[501, 501, 501, 501]
Should I iterate the list or is there a faster way ? thanks
I have a list of sublists, such as:
[[501, 4], [501, 4], [501, 4], [501, 4]]
How can I get rid of the second element for each sublist ? (i.e. 4)
[501, 501, 501, 501]
Should I iterate the list or is there a faster way ? thanks
You can use a list comprehension to take the first element of each sublist:
xs = [[501, 4], [501, 4], [501, 4], [501, 4]]
[x[0] for x in xs]
# [501, 501, 501, 501]
A less pythonic, functional version using map
:
a = [[501, 4], [501, 4], [501, 4], [501, 4]]
map(lambda x: x[0], a)
Less pythonic, since it does not use list comprehensions. See here.