How does Python iterate a for loop?

I tried the following Python code, and this is what I got: It seems that for many changes I am trying to make iterable by changing elem, this does not work.

lis = [1,2,3,4,5] for elem in lis: elem = 3 print lis [1, 2, 3, 4, 5] 

However, if iterators are objects with their own methods (for example, a list), they can be changed in a for loop.

 lis = [[1],[2]] for elem in lis: elem.append(8) print lis [[1, 8], [2, 8]] 

In a for loop, what is the term "elem"? Thanks in advance!

+6
source share
6 answers

The reason this doesn't work is because you don't understand that elem . This is not the object itself, and it cannot even call it a "variable."

This is a name that looks like a label that points to an object. If you simply directly assign over it, you simply rewrite the name to indicate something else. But you still have the original link to the list, so assigning a different value over elem does not change lis itself.

Now, in this case, since all the objects that elem points to are integers, you cannot even change them at all, because integers (and many other types, such as strings or tuples) are immutable . This means that simply after the object has been created, it cannot be modified. This has nothing to do with whether they have β€œmethods” or not (all Python objects have methods, including integers), but whether they are immutable.

However, some objects are mutable , which means that they can be changed. A list of such objects is examples. In the second example, elem is a name that refers to the list objects contained in lis that themselves change. That's why changing them in place (using .append() or .remove() , etc.) works .remove() .

+10
source

The elem variable in your for loop is a reference to the current object at each iteration. Changing this will do nothing; it will just change the value of the elem variable and will change the next time through the loop anyway. To really change the value of an item in a list, you need a link to the list and the index of the item you want to change, and you don't have one.

So what you want to do is something like this:

 for index, elem in enumerate(lis): lis[index] = 3 

So you have elem for the value of the element and index for the position in the list. This saves you from writing lis[index] time to get the values, but you still have to do this to modify the elements.

You can also do:

 for index in xrange(len(lis)): lis[index] = 3 

However, in most situations, this is considered non-python (in particular, what happens if the list becomes longer or shorter during a repeat)?

+5
source

When you assign a new value to the name elem , you simply change the local binding in the for loop. If you want to change the values ​​stored in lis , use map or , for example:

 lis = [3 for elem in lis] 

However, you can change elem attributes (or call methods that do this), just as you can to any other value.

+3
source

Here you are actually modifying the list object in the second example. In the first example, you do not change the number; you replace it. This can be a tricky nuance for new Python users.

Check this:

 >>> x = 1 >>> id(x) 4351668456 >>> x = 2 >>> id(x) 4351668432 

id returns the identifier of the object. As you can see above, the object x modifies both of these times.

 >>> y = [1] >>> id(y) 4353094216 >>> y.append(2) >>> id(y) 4353094216 

Here I am modifying the list, so the list is still the original y object.

So, all this means that when you do elem = 3 , it does not change it, it replaces it. And by now, he is no longer associated with this list.

This is one way to do what you are trying to do. This captures the index, and then modifies the list, not the number.

 lis = [1,2,3,4,5] for idx, elem in enumerate(lis): lis[idx] = 3 print lis [1, 2, 3, 4, 5] 
+3
source

In the first example, you are trying to change an integer and it cannot be used (like strings).

Python variables should be thought of as labels pointing to an object. When you iterate over the inmutables list, elem points to an object that cannot be included, and not to this position in the list, so you cannot change the original list.

In the second case, elem points to an object that can be changed, so you see that the original list is changed.

+1
source

It depends on what type () of Elev is.

In your first case, each element is an int object, and it really works to change it. You change the temporary object when you say: elem = 3 , not the element in the list itself.

In the second case, each item is a list object.

0
source

Source: https://habr.com/ru/post/921991/


All Articles