Python

How to add and remove items from a list in Python

Array variable uses in most of the programming languages to store multiple data. Python has four data types to store multiple data. These are list, tuple, dictionary and set.  The data can be ordered and changed in Python list. The square brackets ([]) are used in Python to declare list like array. The index of the list start from 0.  List works like the reference variables. When a list variable assign to another variable then both variables will point to the same location. This tutorial shows the uses of different Python methods to add and remove data from the Python list.

Methods:

Many methods exist in Python to modify the list. Some common methods to add and remove data in the list are mentioned here.

insert (index,item): This method is used to insert any item in the particular index of the list and right shift the list items.

append (item): This method is used to add new element at the end of the list.

extend (anotherList): The items of one list can be inserted at the end of another list by using this method.

remove (item): This method is used to remove particular item from the list.

pop (index): The method is used to remove item from the list based on index value.

del(): This method is used to remove the particular item of the list or slice the list.

clear(): This method is used to remove all items of a list

Add items into the list:

Different ways to add items in Python list are shown in this part of the tutorial.

Example 1: Insert item using insert() method

Create a python file with the following script to see the use of insert() method. A new item will be inserted in the third position of the list  and the other items will be shifted right after running the script.

# Declare list
listdata = [89, 56, 90, 34, 89, 12]

# Insert data in the 2nd position
listdata.insert(2, 23)

# Displaying list after inserting
print("The list elements are")

for i in range(0, len(listdata)):
  print(listdata[i])

Output:

The following output will appear after running the script.

Example 2: Insert item using append() method

Create a python file with the following script to see the use of append() method. It is mentioned before that append() method inserts data at the end of the list. So, ‘Toshiba’ will be inserted at the end of listdata after running the script.

# Define the list
listdata = ["Dell", "HP", "Leveno", "Asus"]

# Insert data using append method
listdata.append("Toshiba")

# Display the list after insert
print("The list elements are")

for i in range(0, len(listdata)):
  print(listdata[i])

Output:

The following output will appear after running the script.

Example 3: Insert item using extend() method

Create a python file with the following script to see the use of extend() method. Here, two lists are declared in the script which are combined together by using extend() method. The items of the second list  will be added at the end of the first list.

# initializing the first list
list1 = ['html', 'CSS', 'JavaScript', 'JQuery']

# initializing the second list
list2 = ['PHP', 'Laravel', 'CodeIgniter']

# Combine both lists using extend() method
list1.extend(list2)

# Display the list after combing
print ("The list elements are :")

for i in range(0, len(list1)):
  print(list1[i])

Output:

The following output will appear after running the script.

Remove item from the list:

Different ways to remove the item on the Python list are shown in this part of the tutorial.

Example 4: Remove item from the list using the remove method

Create a python file with the following script to see the use remove() method. If the item value that is used as the argument value of remove() method exists in the list the item will be removed. Here, the value, ‘Juice’ exists in the list and it will be removed after running the script.

# Define the list
list = ['Cake', 'Pizza', 'Juice', 'Pasta', 'Burger']

# Print the list before delete
print("List before delete")
print(list)

# Remove an item
list.remove('Juice')

# Print the list after delete
print("List after delete")
print(list)

Output:

The following output will appear after running the script.

Example 5: Remove item from the list using pop method

Create a python file with the following script to see the use of pop() method. Here, 2 is used as the index value for the pop() method. So, the third element of the list will be removed after running the script.

# Define the list
ldata = [ 34, 23, 90, 21, 90, 56, 87, 55]

# Print the before remove
print(ldata)

# Remove the third element
ldata.pop(2)

# Print the list after remove
print(ldata)

Output:

The following output will appear after running the script.

Example 6: Remove item from the list using del method

del() method works similar to pop() method. Create a python file with the following script to see the use of del() method. Here, 0 is used as the index value of the del(). So, the first element of the list will be removed after running the script.

# Define the list
ldata = [ 34, 23, 90, 21, 90, 56, 87, 55]

# Print the before remove
print(ldata)

# Delete the first item using del method
del ldata[0]

# Print the list after remove
print(ldata)

Output:

The following output will appear after running the script.

Example 7: Remove item from the list using clear method

Create a python file with the following script to remove all items of the list. After running the script, clear() method will make the list empty.

# Define the list
ldata = [ 34, 23, 90, 21, 90, 56, 87, 55]

# Print the before remove
print(ldata)

# Remove all items from the list
ldata.clear()

# Print the list after clear
print(ldata)

Output:

The following output will appear after running the script.

Conclusion:

The list is a useful feature of Python programming. List variables are used in the script for various purposes. The ways to modify the list by using various built-in python methods are shown in this tutorial. Many other methods exist in  Python to do other operations in the list, such as sort(), reverse(), count(), etc.

Watch Author’s Video: here

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.