Python list traversal and deletion implementation code, python list Code

Source: Internet
Author: User
Tags python list

Python list traversal and deletion implementation code, python list Code

The list of python can be traversed using a for loop. An error is found during actual development, that is, an error occurs when deleting the list during traversal. for example:

l = [1,2,3,4]for i in l: if i != 4: l.remove(i)print l

The intention of these statements is to clear the list l and leave only element 4, but it is not the result. Next, use index to traverse and delete the list l

l = [1, 2, 3, 4]for i in range(len(l)): if l[i] == 4:  del l[i]print l

In this case, you can traverse and delete the list, but if the list l is changed to l = [1, 2, 3, 4, 5]
If we still follow the above method, imagine that the range starts from 0 to 4, and an element 4 is deleted during the center traversal. At this time, the list is changed to = [1, 2, 3, 5]. At this time, an error will be reported, prompting that the subscript exceeds the expression of the array, because the element is deleted during the traversal mentioned above.

Therefore, you must be careful when deleting elements in the python list during traversal.

You can use filter to filter and return a new list.

l = [1,2,3,4]l = filter(lambda x:x !=4,l)print l

In this way, the element with a value of 4 can be safely deleted. filter requires two parameters, the first is the rule function, and the second parameter requires the input sequence, lambda is used to generate a function. It is a compact method for writing small functions. Generally, simple functions can be used in such a way.

Or

L = [1, 2, 3, 4] l = [I for I in l if I! = 4] // a new sequence is generated, and the value is returned to lprint l.

Or simply create a new list to store the elements to be deleted.

l = [1,2,3,4]dellist = []for i in l: if i == 4:  dellist.append(i)for i in dellist: l.remove(i)

This can also safely delete elements.
Therefore, you must be careful when deleting elements during traversal. In particular, some operations do not report errors, but do not achieve the expected results.

As mentioned above, a new sequence is generated, a value is assigned, etc. You can use the python id () built-in function to view the Object id, which can be understood as the address in the memory, so there is a brief description.
If

L = [,] ll = ll. remove (1) print l // It Must Be [, 4] print ll // What will it be here?

If you use the id function, you will find
Print id (l), id (ll)
Print out the same number, which means they are actually a value. That is to say, the print ll above will be the same as that printed by l, so python has this nature. Just pay attention to it when using it.

This issue is generally mentioned in python.
If you want to update the List itself during traversal
We recommend that you use slice.
L = [1, 2, 3, 4]
For I in l [:]
Some code

Related Article

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.