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

What are the ways to delete elements in the Python list list

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

Share

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

This article focuses on "what are the ways to delete elements from the Python list list". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the ways to delete elements in the Python list list?"

Del: deletes elements based on index values

Del is a keyword in Python that is specifically used to perform delete operations. It can delete not only the entire list, but also some elements in the list. We have explained how to delete an entire list in the Python list, so this section only shows how to delete list elements.

Del can delete individual elements from the list in the format:

Del listname [index]

Where listname represents the list name and index represents the index value of the element.

Del can also delete a contiguous element in the middle in the format:

Del listname [start: end]

Where start represents the starting index and end represents the end index. Del removes elements from index start to end, excluding elements at end location.

[example] use del to delete a single list element:

Lang = ["Python", "C++", "Java", "PHP", "Ruby", "MATLAB"] # use positive index del lang [2] print (lang) # use negative index del lang [- 2] print (lang)

Running result:

['Python',' Ruby', 'MATLAB']

['Python',' Clearing, 'PHP',' MATLAB']

[example] use del to delete a continuous element:

Lang = ["Python", "C++", "Java", "PHP", "Ruby", "MATLAB"] del lang [1: 4] print (lang) lang.extend (["SQL", "C#", "Go"]) del lang [- 5:-2] print (lang)

Running result:

['Python',' Ruby', 'MATLAB']

['Python',' Clearing, 'Go']

Pop (): deletes an element based on the index value

The Python pop () method is used to delete the element at the specified index in the list in the following format:

Listname.pop (index)

Where listname represents the list name and index represents the index value. If the index parameter is not written, the last element in the list is deleted by default, similar to the "out-of-stack" operation in the data structure.

Example of the usage of pop ():

Nums = [40, 36, 89, 2, 36, 100, 7] nums.pop (3) print (nums) nums.pop () print (nums)

Running result:

[40, 36, 89, 36, 100, 7]

[40, 36, 89, 36, 100]

Most programming languages provide a method corresponding to pop (), which is used to add elements to the end of the list, similar to the "stack" operation in a data structure. However, Python is an exception, and Python does not provide a push () method, because you can use append () instead of push ().

Remove (): delete based on element value

In addition to the del keyword, Python also provides the remove () method, which deletes based on the value of the element itself.

It is important to note that the remove () method only deletes the first element that is the same as the specified value, and must ensure that the element exists, otherwise a ValueError error will be thrown.

Example of using the remove () method:

Nums = [40, 36, 89, 2, 36, 100, 7] # first deletion of 36nums.remove (36) print (nums) # second deletion of 36nums.remove (36) print (nums) # deletion of 78nums.remove (78) print (nums)

Running result:

[40, 89, 2, 36, 100, 7]

[40, 89, 2, 100, 7]

Traceback (most recent call last):

File "C:\ Users\ mozhiyan\ Desktop\ demo.py", line 9, in

Nums.remove (78)

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

For the last deletion, since 78 does not cause an error, we'd better judge in advance when we delete the element using remove ().

Clear (): delete all elements of the list

Python clear () is used to delete all elements of the list, that is, to empty the list, see the following code:

Url = list ("/ / www.yisu.com/python/") url.clear () print (url)

Running result:

[]

At this point, I believe that you have a deeper understanding of "what are the methods of deleting elements from the Python list list?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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