[PYTHON] For loop about List Remove method has to say the secret

Source: Internet
Author: User

When learning for loop, have you ever encountered this situation when traversing the list, either the Remove method or the generic del cannot delete the element you want to remove?

First, the list removes the value you want to delete, such as: a=[' h ', ' Z ', ' J ', 1,2,3]->a.remove (h)->a.remove (1)->a=[' z ', ' j ',, 2,3]

Del General method to use a unified subscript, by subscript to delete the corresponding value, example: a=[' h ', ' Z ', ' j ', 1,2,3]->del a[0]->del a[4]->a=[' z ', ' J ', 1,3]

But what do we put him in for a loop?

What we wanted was to delete every element in the list, but why would the list have two values left?

Because the process is as follows:

1, when the first time after the deletion, the following elements moved before, at this time a=[2,3,4,5] #第一个元素可以正常删除
2. The next pointer will then point to the second element of the new list (that is, the third position of the initial state), so that the second position of the initial state is skipped, the initial third position is deleted, and the subsequent element is moved forward again (into [2,4,5])

#指针对应的下标为2, because of the displacement, the second traversal of the value of I directly points to the second position, is also 3
3, the pointer points to the third element of the new list, that is, the initial state of the 5th Element 5, and then 5 is deleted, so only [2,4]

#第一次: A=[1,2,3,4,5]->a=1 The second:a=[2,3,4,5]-> of the pointer takes place i=3 the third time i=5

If the amount of data is not particularly large, you can try to copy the corresponding data, traverse the old data, delete the new data value, so that the implementation of the

This enables the deletion of the list, or you can create a new list, each time the traversal directly add the elements that need to be extracted

Of course, if you want to delete the list, you just need the clear method.

Today, the question of pointers, of course, there is no use, it is good to understand.

[PYTHON] For loop about List Remove method has to say the secret

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.