leafleafleafDocy banner shape 01Docy banner shape 02Man illustrationFlower illustration

Adding and removing elements of a list in Python

Estimated reading: 6 minutes 46 views
Adding and removing elements of a list

Most of the python lists we create are dynamic, which means that after the list is created, the program sets and modifies the elements of the list while it is running. For example, your best friends list may change, either with new best friends joining the list or someone who is no longer your best friend.

Python add list element

When adding a new element to a list, the easiest way is to append the element to the end of the list. Let’s still use the previous example, if we have a new friend Frozy, we need to add a new element to the end of the list.

append() method

The method append() adds the element “Frozy” to the end of the list without affecting other elements in the list.

>>> bestFriends.append("Frozy")
>>> print(bestFriends)
['Christina', 'Mark', 'Justin', 'Jonny', 'Tom', 'Marry', 'Jenny', 'Daniel' , 'Tony', 'Frozy']


We could also create an empty list and then add elements using a series of append() statements. Let’s create an empty list fruits and add elements “apple” “banana” “orange” and “grape” to it.


This can be done with the following code:

>>> fruits=[]
>>> fruits.append("apple")
>>> fruits.append("banana")
>>> fruits.append("orange")
>>> fruits.append("grape" )
>>> print(fruits)
['apple', 'banana', 'orange', 'grape']

First, an empty list is created with fruits = []. Then, on each subsequent line, use the append() method to add a value to the list. Once the list is populated, we can use the print() function to print the entire contents of the list to the screen.

This method of creating lists is common because we often don’t know what data the user wants to store in the program until after the program is run. This allows you to create an empty list to store data, and then add new values ​​to the list when needed.

insert() method

In addition to the append() method, we can also use the insert() method to add new elements to the list. Unlike the append() method, the insert() method can add a new element to any position in the list. To do this, we need to specify the index of the new element. Using the fruits list as an example, suppose we now want to insert “cherry” in the second position.

This can be done with the following code:

>>> fruits.insert(1,"cherry")
>>> print(fruits)
['apple', 'cherry', 'banana', 'orange', 'grape']

After inserting the new element, the list looks like Figure 2.
Figure 2: After inserting a new element
In this example, we use the insert() method.

It should be noted that, as mentioned earlier, the index of the list is counted from 0, so index 1 represents the second position in the list, so “cherry” is inserted before “banana”, and now “cherry” becomes the list The second element of , the index of the following elements increases by 1, “banana” becomes the third element, “orange” becomes the fourth element, and so on.

Python delete list element

We often need to remove one or more elements from a list, for example, let’s say we want to remove “orange” from the fruits list.

del statement

If we already know the index of the element to delete, we can use the del statement. Just put the list element you want to delete after the keyword del. Since “orange” has an index of 3 in the list, put index 3 in square brackets after fruits.

>>> print(fruits)
['apple', 'cherry', 'banana', 'orange', 'grape']
>>> del fruits[3]
>>> print (fruits)
['apple', 'cherry ', 'banana', 'grape']

As you can see, we have successfully removed “orange” from the list of fruits.

remove() method

If we don’t know the index of the element to be removed, but only its value, we can use the remove() method to remove the specified element. Using the fruits list as an example, let’s reassign the list.

>>> fruits=["apple","cherry","banana","orange","grape"]
>>> print(fruits)
['apple', 'cherry', 'banana', 'orange', 'grape']


Again to remove “orange”, this time we use the remove() method and the value put in parentheses is “orange”.

>>> fruits.remove("orange")
>>> print(fruits)
['apple', 'cherry', 'banana', 'grape']

As you can see, we have successfully removed “orange” from the list of fruits.

pop() method

Sometimes, we want to remove an element from the list, and then continue to use its value, this time can use the pop() method. Taking the fruits list as an example, we want to delete the last element in the list and tell everyone what the name of the deleted fruit is.

  • First, we display the output of the elements in the existing fruits list to the screen. You can see that the elements in the list are “apple” “cherry” “banana” “orange” and “grape”.
  • Then, call the pop() method to remove the last element in the list, which is “grape”, and assign it to the variable poppedFruit. Concatenating the string “The popped fruit is ” with the variable poppedFruit and outputting it to the screen, we see “The popped fruit is grape”.
  • Finally, print out the remaining elements in the fruits list, namely “apple” “cherry” “banana” and “orange”. You can see that “grape” no longer exists in the list.
>>> fruits=["apple","cherry","banana","orange","grape"]
>>> print(fruits)
['apple', 'cherry', 'banana', 'orange', 'grape']
>>> poppedFruit=fruits.pop()
>>> print("The popped fruits is "+poppedFruit)
The popped fruits is grape
>>> print(fruits)
['apple', 'cherry', ' banana', 'orange']


Alternatively, we can use the pop() method to remove an element anywhere in the list, as long as we specify the index of the element to remove in parentheses. For example, we want to delete the 3rd element “banana” in the fruits list above, then specify index 2 in the parentheses of pop() as shown below.

>>> otherPoppedFruit=fruits.pop(2)
>>> print("The other popped fruit is "+otherPoppedFruit)
The other popped fruit is banana
>>> print(fruits)
['apple', 'cherry', 'orange ']

As you can see, the element we removed is “banana” and the remaining elements in the fruits list are “apple”, “cherry” and “orange”.

Hint: We see that the pop() method and the del statement have the same effect, so what’s the difference between the two?

If you are not sure which method to use, there is a simple criterion: if you delete an element from the list and you will no longer use the element, use the del statement; if you delete the element and want to continue using its value , use the pop() method.

CONTENTS