Get the App
SLTechnology News&Howtos  ›  Development  › 

How to delete elements from a list in python

Shulou Source: shulou.com Published: 2022-06-01 03:24:35 09月22日 Update

This article mainly introduces how to delete elements from lists in python. The introduction in this article is very detailed and has certain reference value. Interested friends must read it!

#remove: remove elements by their values list1.remove ('milk ')print(list1)#result: [' ham sausage','bread',' cola','instant noodle']# remove: remove elements by their values list1.remove ('milk ')print(list1)

#Result: ['ham sausage',' bread','cola',' instant noodle ']

# pop: Delete elements by index, if no index is filled, the last one will be deleted by default. pop can return deleted elements to list1.pop(-2)print(list1.pop())print(list1)

#Result:

#Instant noodles

# ['ham sausage',' bread','milk']

# del: Delete a single element (same function as pop), delete multiple elements, delete the entire list from memory del list1[-2]print(list1)#Result: ['ham sausage',' bread','milk',' instant noodle '] del list1[1:3]print(list1)#Result: [' ham sausage','cola',' instant noodle '] #Delete all variables with contents from memory del list 1 #clear: Empty list, list is still there, but every content in it print(list1.clear())print(list1)

#Result:

# None

# []

The above is "how to delete elements from lists in python". Thank you for reading it! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Tags: Elements results instant noodles ham sausage content milk bread cola memory article index value interest function single variable multiple guys buddies Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Apple macOS Shulou Information Redmi