Here is the code:
for m, n in ("example_string", True):
print(m, n)
This code doesn't work. Interpreter says:
But how to unpack this 2-items tuple in FOR
loop?
Desirable output is:
example_string True
Here is the code:
for m, n in ("example_string", True):
print(m, n)
This code doesn't work. Interpreter says:
FOR
loop?Desirable output is:
example_string True
You need to unpack it first.
m, n = ("example_string", True)
If the tuple contained iterables itself, then you could unpack it in the loop:
for m, n in (('x','y'), (x,y)): # this works
You can't iterate over a 2-length tuple and unpack it into a tuple at the same time. Try this:
m, n = ("example_string", True)
print(m, n)
If you want to unpack your tuple inside the for-loop, each item in the iterable must be a 2-tuple.
for m,n in [(1,2), (3,4)]:
print(m,n)
This would print:
1 2
3 4