Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the remove () function in python list

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Python list in the use of the remove() function, for this problem, this article details the corresponding analysis and solution, hoping to help more want to solve this problem of small partners to find a simpler and easier way.

1. basic use

The remove() function removes a specified element from a list

syntax

list.remove( element )

parameters

element: Any data type (number, string, list, etc.)

2. Delete common type elements

Delete a number or string that "exists" in a list

list1 = ['zhangsan',' lisi', 1, 2]list1.remove(1) #remove number print(list1)list1.remove('zhangsan')#remove string print(list1)

Output:

['zhangsan', 'lisi', 2]

['lisi', 2]

If the element you want to delete does not exist in the list, an error will be reported.

list1 = [1, 2, 3]list1.remove(4)

Output:

Traceback (most recent call last):

File "E:/data/PrCharm/test1/55.py", line 2, in

list1.remove(4)

ValueError: list.remove(x): x not in list

#Value error: value to delete is not in list

3. Delete Object Type Element

Delete an object element that "exists" in a list (list, ancestor, etc.)

list1 = [1, 2, [3, 4], (5, 6)]a = [3, 4]b = (5, 6)list1.remove(a) #remove list type print(list1)list1.remove(b) #remove Yuanzu type print(list1)

Output:

[1, 2, (5, 6)]

[1, 2]

Note that: remove the premise of deleting the object type is that a certain "element" in the list is an object type, the following way will report an error

list1 = [1, 2, 3]list1.remove([1,2])

Output:

Traceback (most recent call last):

File "E:/data/PrCharm/test1/55.py", line 2, in

list1.remove([1,2])

ValueError: list.remove(x): x not in list

#Value error: value to delete is not in list

4. Delete one element at a time

In the example above, list [1, 2] appears to exist in list [1, 2, 3], but in fact, the remove() function determines whether an element exists in the list by matching "a single element" in the list, not the entire list.

The remove() function iterates through each element in the list to see if there are any matches, and deletes only if there are matches, which means that the remove() function can delete only one element at a time.

You can't delete yourself, because you're not in your element.

list1 = [1, 2, 3, 4, 5]print ('Am I in myself? ', list1 in list1)list1.remove(list1)

Output:

Traceback (most recent call last):

File "E:/data/PrCharm/test1/55.py", line 3, in

list1.remove(list1)

ValueError: list.remove(x): x not in list

Am I in myself? False

5, Python list remove method considerations

Why didn't you delete all the elements from the list?

Explanation:

In execution order, after the first space is removed, the following elements move forward (becomes ['whitespace',' whitespace','12',' 23']), the pointer next points to the second element of the new list (i.e., the third space in the initial state), so that the second space in the initial state is skipped, the initial third space is deleted, and then the following elements are moved forward again (becoming ['space',' 12','23']), the pointer points to the third element of the new list, i.e., the fifth element in the initial state, 23, and then 23 is deleted, leaving only [' space','12']

If you want to exclude some elements from the initial list, how do you do that?

From the above situation, if you execute delete operation on the list while traversing the list, it will cause unexpected results. What about traversing the initial list and executing delete operation on the copy of the initial list?

The above results show that the expected results have not been achieved. Why not?

The problem lies in the sentence copy=ls, which simply makes copy and ls point to the same piece of memory (i.e. shallow copy), and does not execute the series of operations [open a new piece of memory, and copy the contents of ls memory to the new memory, and then make copy point to the newly opened memory, i.e. deep copy]. So the remove operation performed on copy is essentially the same piece of memory as traversing the ls list, so the result is similar to the previous example.

To solve this problem, there are three ways:

(1)

ls=[' ',' ',' ','12','23','abc','aa']copy=[' ',' ',' ','12','23','abc','aa']

This method is feasible for all elements of a known list, and the number of elements is small, and the structure is simple. It is not feasible in other cases.

(2)Deepcopy method for introducing copy module:

(3)Also prepare an empty list. When traversing the initial list, add the elements that meet the conditions to the empty list one by one (using the append method of the list).

This method is the opposite of the remove method in thinking, but performs similar operations and has similar time complexity to the remove method, without introducing a copy module.

In addition, for the remove method of lists, the instructions given in the second edition of Python Basic Tutorial are:

The remove method removes the first occurrence of a value in a list:

>>>x=['to','be','or','not','to','be']

>>>x.remove('be')

>>>x

['to','or','not','to','be']

About the python list in the use of the remove() function to share the answer to the problem here, I hope the above content can be of some help to everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report