Iterate Through a Python List of Tuples



Easiest way is to employ two nested for loops. Outer loop fetches each tuple and inner loop traverses each item from the tuple. Inner print() function end=’ ‘ to print all items in a tuple in one line. Another print() introduces new line after each tuple.

Example

L=[(1,2,3), (4,5,6), (7,8,9,10)]
for x in L:
  for y in x:
    print(y, end=' ')
  print()

Output

1 2 3

4 5 6

7 8 9 10
Updated on: 2020-02-20T08:07:37+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements