In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.