Adding and removing items from a list in Python

problem

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.

SOLUTION

Adding an item

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

list_io.py
				
					numbers = [] # empty list
				
			

We can also create an empty list using the list() function as follows:

list_io.py
				
					numbers = list() # creates an empty list
				
			

We wish to add a number, e.g. 5.

list_io.py
				
					numbers.append(5) # adds it to the end of the list
				
			

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

list_io.py
				
					numbers.append(7)
				
			

Let’s print the list

list_io.py
				
					print(numbers)
				
			
output
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:

list_io.py
				
					numbers.insert(0,3) # adds the number 3 at index 0
				
			
output
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:

list_io.py
				
					numbers.append([9,11,13]) # add the list [9,11,13] to the current list
				
			
output
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:

list_io.py
				
					len(numbers)
				
			
output
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:

list_io.py
				
					removed_item = numbers.pop() # removes and returns the last item
				
			
list_io.py
				
					print(removed_item)
				
			
output
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:

list_io.py
				
					second_item = numbers.pop(1) # remove and get the second item
print(second_item)
				
			
output
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:

list_io.py
				
					numbers.remove(7)
				
			

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

output
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.

conclusion

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!

Facebook
Twitter
LinkedIn
Reddit
Picture of Ellion

Ellion

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

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x