I iterate over the list of tuples and, each iteration, add some additional elements to the current loop variable, and then perform the action with the new list.
The temptation is to change the loop variable to include additional elements, and then do something with it.
Consider the following code snippet:
required_part = (0, 4)
optional_part = [(1, 2), (1, 3), (2,3)]
for x in optional_part:
x += required_part
x = sorted(x)
print(x)
But something about mutating a variable cycle during a cycle makes me feel awkward.
Are there situations where a mutation in a loop variable will produce unexpected results, or can I just stop worrying?
Note: there seems to be a lot of discussion about changing iteration. This question is more likely related to changing the loop variable.
source
share