Iterate and update a list in python

I cannot understand why the following code goes indefinitely loop (when I do not use the list of copies)

list = ["Mohit","kumar","sffsfshfsd"]
for w in list:
    if(len(w)) > 5:
        list.insert(0,w)
    print("inside loop")

print(list)  

The above code prints inside the loop indefinitely.

Now, if instead of a list, I use a list of copies, as shown below, works fine.

list = ["mohit","kumar","sffffgssddf"]

for w in list[:]:
    if len(w) > 5:
        list.insert(0,w)
    print("inside loop")

print(list)  

Now I read in the python documentation that I will get this behavior, but I want to understand the reason for this. Thanks in advance.

+4
source share
6 answers

for w in list ( iter(list)) . - , , , . / .

0 1 , 6. 2, , "sffsfshfsd" list. list - 3: "sffsfshfsd". , (3), , , 3, 4... .

w in list[:] ( ) . , , , .

PS: Python ( C), , ( ). Python, cpython/listobject.c:

, 0

2797 static PyObject *
2798 list_iter(PyObject *seq)
2799 {
....
2806     it = PyObject_GC_New(listiterobject, &PyListIter_Type);
....
2809     it->it_index = 0;
....
2813     return (PyObject *)it;
2814 }

next it->it_index ,

2831 static PyObject *
2832 listiter_next(listiterobject *it)
2833 {
....
2844         item = PyList_GET_ITEM(seq, it->it_index);
2845         ++it->it_index;
....
2847         return item;
....
2853 }

?

+5

, , , while.

lst = ["Mohit", "kumar", "sffsfshfsd"]
pos = 0
while pos < len(lst):
  word = lst[pos]
  print('lst=%s pos=%d word=%s' % (lst, pos, word))
  if len(word) > 5:
    lst.insert(0, word)
  pos += 1

, :

lst=['Mohit', 'kumar', 'sffsfshfsd'] pos=0 word=Mohit
lst=['Mohit', 'kumar', 'sffsfshfsd'] pos=1 word=kumar
lst=['Mohit', 'kumar', 'sffsfshfsd'] pos=2 word=sffsfshfsd
lst=['sffsfshfsd', 'Mohit', 'kumar', 'sffsfshfsd'] pos=3 word=sffsfshfsd
lst=['sffsfshfsd', 'sffsfshfsd', 'Mohit', 'kumar', 'sffsfshfsd'] pos=4 word=sffsfshfsd
lst=['sffsfshfsd', 'sffsfshfsd', 'sffsfshfsd', 'Mohit', 'kumar', 'sffsfshfsd'] pos=5 word=sffsfshfsd
lst=['sffsfshfsd', 'sffsfshfsd', 'sffsfshfsd', 'sffsfshfsd', 'Mohit', 'kumar', 'sffsfshfsd'] pos=6 word=sffsfshfsd
lst=['sffsfshfsd', 'sffsfshfsd', 'sffsfshfsd', 'sffsfshfsd', 'sffsfshfsd', 'Mohit', 'kumar', 'sffsfshfsd'] pos=7 word=sffsfshfsd
lst=['sffsfshfsd', 'sffsfshfsd', 'sffsfshfsd', 'sffsfshfsd', 'sffsfshfsd', 'sffsfshfsd', 'Mohit', 'kumar', 'sffsfshfsd'] pos=8 word=sffsfshfsd
lst=['sffsfshfsd', 'sffsfshfsd', 'sffsfshfsd', 'sffsfshfsd', 'sffsfshfsd', 'sffsfshfsd', 'sffsfshfsd', 'Mohit', 'kumar', 'sffsfshfsd'] pos=9 word=sffsfshfsd
...

( , , .)

, 'sffsfshfsd' , .

, , , .

, :

  if len(word) > 5:
    lst.insert(0, word)
    pos += 1  # account for the extra word
  pos += 1

:

  if len(word) > 5:
    lst.insert(0, lst.pop(pos))  # don't change len(lst)
+4

, "sffsfshfsd" , , .

+2

, . , . , for , .

+2

:

: , ( , .. ). , , . . , Suite ( ) , ( ). , , . , , , , ,

for x in a[:]:
    if x < 0: a.remove(x)

A Python .

, sffsfshfsd (.. 2), , , sffsfshfsd 3 . ...

, .

lst = ["Mohit","kumar","sffsfshfsd"]
for i, w in enumerate(lst):
    print("Index: {i} | List: {list}".format(i=i, list=lst))
    if(len(w)) > 5:
        lst.insert(0, w)

:

Index: 0 | List: ['Mohit', 'kumar', 'sffsfshfsd']
Index: 1 | List: ['Mohit', 'kumar', 'sffsfshfsd']
Index: 2 | List: ['Mohit', 'kumar', 'sffsfshfsd']
Index: 3 | List: ['sffsfshfsd', 'Mohit', 'kumar', 'sffsfshfsd']
Index: 4 | List: ['sffsfshfsd', 'sffsfshfsd', 'Mohit', 'kumar', 'sffsfshfsd']
Index: 5 | List: ['sffsfshfsd', 'sffsfshfsd', 'sffsfshfsd', 'Mohit', 'kumar', 'sffsfshfsd']
Index: 6 | List: ['sffsfshfsd', 'sffsfshfsd', 'sffsfshfsd', 'sffsfshfsd', 'Mohit', 'kumar', 'sffsfshfsd']
+2

, . , python (, , - Python)

. , , , . ( , , , )

[:], . , , , for ( ).

:

list = ["mohit","kumar","sffffgssddf"]
test = list
list.append("test")
print test 
#['mohit', 'kumar', 'sffffgssddf', 'test']

#clear data, let try [:]
list = ["mohit","kumar","sffffgssddf"]
test = list[:]
list.append("test")
print test 
#['mohit', 'kumar', 'sffffgssddf']

, , for . , . , , .

, .

+1

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


All Articles