Adding and removing items from a list in Python


You are using a list in your Python program and wish to add and remove items. In this post we will see built-in functions that allow us to manipulate the items in the list.


Adding an item

Let’s suppose we have a list named numbers that is currently empty as follows:
					numbers = [] # empty list

We can also create an empty list using the list() function as follows:
					numbers = list() # creates an empty list

We wish to add a number, e.g. 5.
					numbers.append(5) # adds it to the end of the list

Let’s add another number, e.g. 7

Let’s print the list
print list in python

append() adds the item to the end of the list.

Adding an item at the beginning

Let’s suppose I wish to add number 3 at the beginning, I’ll use the insert() function:
					numbers.insert(0,3) # adds the number 3 at index 0
list.insert python example

The insert() function takes 2 parameters:

  • the index where to insert the new item
  • and the second parameter is the item to be added.

Recap: In Python’s list we can have items of any data-type.

Adding multiple items

We can use append() again to add a list of items, for example:
					numbers.append([9,11,13]) # add the list [9,11,13] to the current list
python append list in a list example

The list now contains 4 items: 3 integers and one list.

We can use len() to determine the size of the list, for example:
size of list in Python example
Removing an item

To remove the last item of the list we can use the pop() function. For example:
					removed_item = numbers.pop() # removes and returns the last item
list pop example python

The last item was the list [9,11,13] added above. The pop() function removes and returns the item in case we wish to further use it we store it in a variable (removed_item).

Remove item by index

Currently the list contains the following items:

print list Python example

The pop() function can accept a parameter which acts as the index of the item to be removed. For example to remove the second item:
					second_item = numbers.pop(1) # remove and get the second item
list popped item Python example
Removing an item using its value

Let’s suppose we wish to remove number 7 from the list. We can use the remove() function with a parameter being the item to be removed. For example:

It’s important to note here that the remove() function does not return the item, only removes it.

list remove() Python example

The list now contains only one item: 3.

Attempting to Remove an item that does not exist

Using the remove() with an item that is not in the list will result in a ValueError.

python list.remove() error

Such errors that crash the program can be handled using the try catch statement.


In this post we saw two ways for adding items in a list and three ways for removing items from a list and their possible errors.

Share it!

Picture of Ellion


Professional IT consultant, writer, programmer enthusiast interested in all sorts of coding.
Eats all cookies 🍪

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x