How to Replace Items in a Python List

You can use a list comprehension to replace items in a Python list:

my_list = ['item 1', 'item 2', 'item 3',...]
my_list = ['new item' if i=='old item' else i for i in my_list]

To better understand how to replace items in a Python list, you’ll see the following 3 scenarios about:

  1. Replacing an item with another item
  2. Replacing multiple items with another item
  3. Replacing multiple items with multiple items

The Example

To start with a simple example, let’s create the following list of fruits.

my_list = ['Banana','Banana','Apple','Mango','Banana','Mango','Mango','Apple']

print(my_list)

This is how the list would look like:

How to Replace Items in a Python List

(1) Replace an item with another item

Suppose that you want to replace Banana with Pear.

You can then use a list comprehension to perform the replacement:

my_list = ['Banana','Banana','Apple','Mango','Banana','Mango','Mango','Apple']

my_list = ['Pear' if i=='Banana' else i for i in my_list]

print(my_list)

You’ll now see that Banana was replaced with Pear in 3 locations:

How to Replace Items in a Python List

(2) Replace multiple items with another item

What if you want to replace multiple items with another item?

For example, let’s say that you want to replace Banana and Apple with Pear.

In that case, you can use the following syntax to perform the replacement:

my_list = ['Banana','Banana','Apple','Mango','Banana','Mango','Mango','Apple']

my_list = ['Pear' if i in ['Banana','Apple'] else i for i in my_list] 

print(my_list)

As you can see, both Banana and Apple got replaced with Pear in 5 locations:

Replace Values in a Python List

Alternatively, you can achieve the same results with the help of pipe (“|”):

my_list = ['Banana','Banana','Apple','Mango','Banana','Mango','Mango','Apple']

my_list = ['Pear' if (i=='Banana') | (i=='Apple') else i for i in my_list] 

print(my_list)

You’ll get the same results:

Replace Values in a Python List

(3) Replace multiple items with multiple items

For this scenario, the goal is to:

  • Replace Banana with Pear
  • Replace Apple with Watermelon

Here is the syntax that you can use:

my_list = ['Banana','Banana','Apple','Mango','Banana','Mango','Mango','Apple']

my_list = ['Pear' if i=='Banana' else 'Watermelon' if i=='Apple' else i for i in my_list] 

print(my_list)

You’ll now see that Banana was replaced with Pear (as highlighted in red), while Apple was replaced with Watermelon (as highlighted in green):

How to Replace Values in a Python List

Working with Numeric Data

So far, you have seen how to work with texts/strings in your lists.

But what your list contains numeric data?

For instance, let’s create the following list that contains numeric data (i.e., integers):

my_list = [22,33,77,22,33]

print(my_list)

Here is the new list:

Numeric data

Suppose that you want to replace the value of 22 with 99.

You can then use the syntax below to perform the replacement (note that unlike the previous scenarios, it’s not necessary to use quotes around numeric values):

my_list = [22,33,77,22,33]

my_list = [99 if i==22 else i for i in my_list] 

print(my_list)

As you can see, the numeric value of 22 was replaced with 99 in 2 locations:

Working with numeric data