-5

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

yob-v-u
  • 119
  • 4
  • 15
  • 3
    Have you trying anything already? – DeepSpace Aug 08 '16 at 06:30
  • 4
    Possible duplicate of [How to extract the n-th elements from a list of tuples in python?](http://stackoverflow.com/questions/3308102/how-to-extract-the-n-th-elements-from-a-list-of-tuples-in-python) – miradulo Aug 08 '16 at 06:31
  • 1
    A good place to learn more about looping through lists : [List Comprehensions](http://stackoverflow.com/documentation/python/196/comprehensions#t=201608080634467306627) – Mahdi Aug 08 '16 at 06:35

3 Answers3

0

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
Rakesh Kumar
  • 4,319
  • 2
  • 17
  • 30
0

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)]
Daniel
  • 980
  • 9
  • 20
-2

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)

demonplus
  • 5,613
  • 12
  • 49
  • 68
  • Note: It is _tuple_, not _touple_. And what if OP wanted to operate on the second elements, not just print them? Perhaps place them in a new list? Currently your answer provides less detail than the answers on the [linked duplicate](http://stackoverflow.com/questions/3308102/how-to-extract-the-n-th-elements-from-a-list-of-tuples-in-python). – miradulo Aug 08 '16 at 06:40