Let's say I have a list like so: [(1, (1,2)), (2, (23, -10)), (3, (4, 5))...]
I want to get (1,2), (23, -10), etc
edit: Thanks for help. I didn't know about list comprehension as I'm not too familiar with python
Let's say I have a list like so: [(1, (1,2)), (2, (23, -10)), (3, (4, 5))...]
I want to get (1,2), (23, -10), etc
edit: Thanks for help. I didn't know about list comprehension as I'm not too familiar with python
Try Something like this:-
Here is List and get other list of tuples:-
a = [(1, (1,2)), (2, (23, -10)), (3, (4, 5))...]
b = map(lambda item: item[1], a)
print b
This will solve your problem.
Here is list of tuples where second elem of each tuple is also a tuple. To get that second elem we are going to use lambda that is going to take elements from list and just return second item from that element, in this case being desired tuple. The map
function also creates a list of returned values.
>>> list_of_nested_tuples = [(1, (1, 2)), (2, (23, -10)), (3, (4, 5))]
>>> b = map(lambda item: item[1], list_of_nested_tuples)
>>> b
[(1, 2), (23, -10), (4, 5)]
Take note that it would be more clear to just use list comprehension like so
>>> [elem[1] for elem in list_of_nested_tuples]
[(1, 2), (23, -10), (4, 5)]
Yes, you can iterate over all tuples and then take the second element:
list = [(1, (1,2)), (2, (23, -10)), (3, (4, 5))]
for elem in list:
print(elem[1])
In each iteration elem value its (1,(1,2)) -> (2,(23,-10))
-> ....
Then you take the second item of the tuple (index 1)