0
In Python it is somewhat common to go through items of a everlasting handling or verifying the existence of a particular item, or whatever the operation is. I am reading the Python documentation again and I come across something that when seen for the first time I did not believe to be something say relevant. For example:
>>> animals = ['Cat', 'Dog', 'Elephant']
>>> for animal in animals:
... print(animal, len(animal))
Cat 3
Dog 3
Elephant 8
As you can see above a list containing animal names and I print the current animal in the loop, as well as the number of characters in it. The only operation I do in this example is the printing of your name and the number of characters, I do not try to change the value of them in any way. My doubt exactly when I think about changing these values.
>>> animals = ['Cat', 'Dog', 'Elephant']
>>> for animal in animals:
... if len(animal) > 3:
animal = animal[:3]
print(animal, len(animal))
Cat 3
Dog 3
Ele 3
>>> print(animals)
The output inside the loop as you can see demonstrates that the variable in the loop was changed, but when printing the list after the loop I see that the list itself was not. It baffled me, because frankly what I’m showing here as examples is nothing more than things that came to mind that I believe we’ve all thought about doing which is altering a variable during a loop of repetition, another fact is that studying Python we learned that some of its most basic types like string
, int
, float
and even the tuplas
are immutable, that is, because the reallocation in the current variable of the loop did not generate a new string (in case a string three-character), so thinking I thought I’d check whether the variable
in the current loop was the same as the list being iterated, as follows:
>>> animals = ['Cat', 'Dog', 'Elephant']
>>> for animal in animals:
... print(animal, id(animal))
Cat 140226536948264
Dog 140226536948040
Elephant 140226536875248
>>> animals[0], id(animals[0])
Cat 140226536948264
>>> animals[1], id(animals[1])
Dog 140226536948040
>>> animals[2], id(animals[2])
Elephant 140226536875248
You got it? It’s the same transponders. I don’t know in other languages but in the Python documentation it is described that one should create a copy of the iterable before the loop since it does not do this implicitly, but my doubt is still this because I can not change the current loop item for
.
I couldn’t reproduce your example. Here it showed size 3 for everyone, as you expected: https://repl.it/repls/StrikingIllustriousNumbers
– Woss
During iteration the item is changed to 3 characters but after iteration the list itself is unchanged.
– ThiagoO
Yes, but that’s not what’s in your question. You changed the variable and displayed it inside the loop. By your text, you asked why
animal
continued to beElephant
inside the same loop assigninganimal = animal[:3]
. If the intention was to ask why the change is not reflected in the original list, I think you need to rephrase the question.– Woss
Sorry if it wasn’t clear I changed the question.
– ThiagoO