Python - Use Pop/Del/Remove on Lists

by matt392 in Circuits > Software

17 Views, 0 Favorites, 0 Comments

Python - Use Pop/Del/Remove on Lists

difference-between-del-remove-and-pop-on-lists.png
# Program that uses the functions pop, del and remove on lists

LinuxList = ["openSuse", "Debian", "Gentoo", "antiX", "Kali", "LinuxMint", "MxLinux", "Ubuntu", "Qubes"]

print ("LinuxList is: ", LinuxList, "\n")

print ("Using 'pop' to remove an element from a list.")
ThirdElement = LinuxList.pop(2)
print ("The LinuxList is now: ", LinuxList)
print ("The element that was removed is: ", ThirdElement)
print ("\n")

print ("Using 'del' to remove element from a list.")
del LinuxList[3]
print ("LinuxList now: ", LinuxList)
print ("\n")

print ("Using 'remove' to delete an element from a list using a string match.")
LinuxList.remove("antiX")
print ("LinuxList is now: ", LinuxList)
print ("\n")

print ("Removing multiple elements from a list.")
del LinuxList[3:5]
print ("LinuxList is now: ", LinuxList)