As we already know, if we have a list which contains equal tuple in size then we can iterate through like below,
list1 =[(1,2),(3,4),(5,6)]
for (i,j) in list1:
print(i,j)
what if I have a list of tuple as below:
list1 =[(1,2,3),(4,5),(7,8,9),(10,11)]
instead of using nested for loop, is there any other way to iterate through all element?
If you already know the minimum size, you can unpack the minimum and the remainder:
>>> list1 =[(1,2,3),(4,5),(7,8,9),(10,11)]
>>> for i, j, *r in list1:
... print(i, j, r)
...
1 2 [3]
4 5 []
7 8 [9]
10 11 []
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With