Get the App
SLTechnology News&Howtos  ›  Development  › 

How to delete list elements del,pop (), remove () and clear () in python

Shulou Source: shulou.com Published: 2022-06-01 06:49:13 09月14日 Update

This article mainly introduces how to delete list elements del,pop (), remove () and clear () in python. It is very detailed and has certain reference value. Friends who are interested must finish reading it!

1. Del delete list

Del is not a method, but 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. You can delete not only individual elements, but also element segments. And del removes lists or list elements directly from memory.

Let's first take a look at the results returned by deleting the entire list:

Name1 = ['python',' java', 'php',' MySql', 'Clover,' Che'] print (name1) del name1 print (name1)

Return the result:

['python',' java', 'php',' MySql', 'Centrex,' Che, 'Che']

Traceback (most recent call last):

File "C:/Users/Administrator/Desktop/python knowledge Summary / python Basics / 9-3. Delete list elements .py", line 5, in

Print (name1)

NameError: name 'name1' is not defined

The result returned here is that name1 is not defined, indicating that the list has been completely deleted.

The following deletes the element with the specified index value:

Name1 = ['python',' java', 'php',' MySql', 'Centrum,' Che, 'Che'] del name1 [3] print (name1)

Return the result:

['python',' java', 'php',' Centrex, 'Che', 'Che']

Remove from the reverse index:

Name1 = ['python',' java', 'php',' MySql', 'Centrum,' Che, 'Che'] del name1 [- 3] print (name1)

Return the result:

['python',' java', 'php',' MySql', 'Che,' Che']

Note: the positive order starts at 0 and the reverse order starts at-1.

The following is the deletion of the specified interval element:

Name1 = ['python',' java', 'php',' MySql', 'Centrex,' Che, 'Che'] del name1 [3:5] print (name1)

Return the result:

['python',' java', 'php',' Che, 'Che']

Second, the pop () method deletes the list element name1 = ['python',' java', 'php',' MySql', 'Centers'] name1.pop (0) # deletes the first element print (name1) name1.pop (- 1) # deletes the last element print (name1) name1.pop () # deletes the last element print (name1) by default

Return the result:

['java',' php', 'MySql',' Centrex, 'Che', 'Che']

['java',' php', 'MySql',' Centrex,'C']

['java',' php', 'MySql',' Clippers']

Third, the remove () method deletes list elements

Remove () can only delete the list element or the first element of the specified value, which are conditional and related, that is, if there are two identical values in the list, only the first will be deleted, if the element does not have a return ValueError error.

Name1 = ['python',' java', 'php',' MySql', 'Clover,' Che, 'php',' Che'] name1.remove ('php') print (name1) name1.remove (' php') print ('php') print (name1)

Return the result:

['python',' java', 'MySql',' Clearing, 'php',' Clearing']

['python',' java', 'MySql',' Centrex, 'Che', 'Che']

Traceback (most recent call last):

File "C:/Users/Administrator/Desktop/python knowledge Summary / python Basics / 9-3. Delete list element .py", line 32, in

Name1.remove ('php')

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

4. Clear () deletes list elements

The above method deletes some of the elements in the list, and the clear () method clears all the elements in the list.

Name1 = ['python',' java', 'php',' MySql', 'Centrex,' Che, 'php',' Che'] name1.clear () print (name1)

Return result: []

The above is all the content of the article "how to delete list elements del,pop (), remove () and clear () in python. Thank you for reading!" Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Element result method knowledge index two reverse order content foundation article same that is value key keyword interest memory interval single Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS vpn MySQL OPPO Reno MariaDB