How we can 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

Advertisements