-2

I want to display 2nd value in tuple of tuples.What are the multiple ways to make sure this happens?

tuple =((1,"qwerty","poiuyt"),(2,"mnbvc","waxds"))
Madhav Parikh
  • 73
  • 2
  • 9

1 Answers1

1

tuple unpacking is always clean:

>>> t =((1,"qwerty","poiuyt"),(2,"mnbvc","waxds"))
>>> tuple(y for x, y, z in t)
('qwerty', 'mnbvc')

You can use indexing as well:

>>> tuple(x[1] for x in t)
('qwerty', 'mnbvc')

Using operator.itemgetter:

>>> from operator import itemgetter
>>> tuple(map(itemgetter(1), t))
('qwerty', 'mnbvc')

Using lambda:

>>> tuple(map(lambda x: x[1], t))
('qwerty', 'mnbvc')

Plus many more other variations.

RoadRunner
  • 25,803
  • 6
  • 42
  • 75