Take a=[1,2,3] For example, it seems to use Del, remove, pop an element 2 after a is all for [1,3], as follows:
- >>> a=[1,2,3]
- >>> A.remove (2)
- >>> A
- [1, 3]
- >>> a=[1,2,3]
- >>> del a[1]
- >>> A
- [1, 3]
- >>> a= [1,2,3]
- >>> A.pop (1)
- 2
- >>> A
- [1, 3]
- >>>
So what's the difference between Python and the list del, remove, and pop operations?
First, remove is the first element that meets the criteria. Does not delete a specific index. The following example: This article from the Novell fan website http://novell.me
- >>> a = [0, 2, 2, 3]
- >>> A.remove (2)
- >>> A
- [0, 2, 3]
For Del, it is deleted based on the index (where the element is), as in the following example:
- >>> a = [3, 2, 2, 1]
- >>> del a[1]
- [3, 2, 1]
The 1th element is a[0]--is counted starting at 0. Then a[1] refers to the 2nd element, which is the value 2.
Finally, we'll look at POPs.
- >>> a = [4, 3, 5]
- >>> A.pop (1)
- 3
- >>> A
- [4, 5]
Pop returns the value that you popped.
So use the appropriate method according to your specific needs. content from Http://novell.me
In addition, if they go wrong, the error mode is different. Note the following differences:
- >>> a = [4, 5, 6]
- >>> A.remove (7)
- Traceback (most recent):
- File "<stdin>", line 1, in <module>
- ValueError:list.remove (x): X not in list
- >>> del a[7]
- Traceback (most recent):
- File "<stdin>", line 1, in <module>
- Indexerror:list Assignment index out of range
- >>> A.pop (7)
- Traceback (most recent):
- File "<stdin>", line 1, in <module>
- Indexerror:pop index out of range
The Del, Remove,pop difference of the python array