How we can iterate through a Python list of tuples?

PythonServer Side ProgrammingProgramming

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
raja
Published on 11-Jan-2018 18:55:19
Advertisements