I had a problem with a for loop in a python script. The problem is meanwhile solved, but I do not understand the necessity of the comma that solved the issue.
this was the faulty for loop:
variable= (["abc.com", ["", "test"]])
for a,b in variable:
print(a,b)result:
Traceback (most recent call last):
File "", line 1, in
ValueError: too many values to unpack (expected 2)
this repaired the faulty for-loop:
variable= (["abc.com", ["", "test"]],)
for a,b in variable:
print(a,b)result:
abc.com ['', 'test']
Why is this comma necessary before the closing bracket? If I extend the content inside the variable, there is no comma necessary at the end.
without comma at the end:
variable= (["abc.com", ["", "test"]], ["xyz.com", ["", "test2"]])
for a,b in variable:
print(a,b)result:
abc.com ['', 'test']
xyz.com ['', 'test2']
with comma at the end:
variable= (["abc.com", ["", "test"]], ["xyz.com", ["", "test2"]],)
for a,b in variable:
print(a,b)result:
abc.com ['', 'test']
xyz.com ['', 'test2']
Any idea why there is sometimes the last comma necessary and sometimes not?
Thanks