I stumbled across the following code:
for i, a in enumerate(attributes):
labels.append(Label(root, text = a, justify = LEFT).grid(sticky = W))
e = Entry(root)
e.grid(column=1, row=i)
entries.append(e)
entries[i].insert(INSERT,"text to insert")
I don’t understand the i, a bit, and searching for information on for didn’t yield any useful results. When I try and experiment with the code I get the error:
ValueError: need more than 1 value to unpack
Does anyone know what it does, or a more specific term associated with it that I can google to learn more?
You could google "tuple unpacking". This can be used in various places in Python. The simplest is in assignment:
In a for-loop it works similarly. If each element of the iterable is a
tuple, then you can specify two variables, and each element in the loop will be unpacked to the two.The
enumeratefunction creates an iterable of tuples, so it can be used this way.