+ 3
What does multiple variables mean in Python 'for loop' ?
I saw this 'for loop' in a code somewhere, I wonder what it is and how it works. It has 4 variables in parenthesis, usually I've seen and used only one variable which iterates through a list or a range. for (x, y, w, h) in faces:
2 odpowiedzi
+ 9
you can see it like this:
nums = [[1,2,3,4],[5,6,7,8]]
for (a,b,c,d) in nums:
#for a,b,c,d in nums: # it can be used with or without parenthesis
print(a,b,c,d)
# output:
'''
1 2 3 4
5 6 7 8
'''
so the sub-lists in nums will be splitted to individual variables a, b, c, d.
+ 17
Tanish Kushwaha maybe the following page helps:
https://stackoverflow.com/questions/18648626/for-loop-with-two-variables#18648679