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_new_list = ["new item" if i == "old item" else i for i in my_list]

In this guide, you’ll see 3 cases of:

  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, 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:

['Banana', 'Banana', 'Apple', 'Mango', 'Banana', 'Mango', 'Mango', 'Apple']

Case 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_new_list = ["Pear" if i == "Banana" else i for i in my_list]

print(my_new_list)

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

['Pear', 'Pear', 'Apple', 'Mango', 'Pear', 'Mango', 'Mango', 'Apple']

Case 2: Replace multiple items with another item

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

For example, to replace Banana and Apple with Pear:

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

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

print(my_new_list)

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

['Pear', 'Pear', 'Pear', 'Mango', 'Pear', 'Mango', 'Mango', 'Pear']

Alternatively, you can achieve the same results using pipe (“|”):

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

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

print(my_new_list)

You’ll get the same results:

['Pear', 'Pear', 'Pear', 'Mango', 'Pear', 'Mango', 'Mango', 'Pear']

Case 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_new_list = ["Pear" if i == "Banana" else "Watermelon" if i == "Apple" else i for i in my_list]

print(my_new_list)

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

['Pear', 'Pear', 'Watermelon', 'Mango', 'Pear', 'Mango', 'Mango', 'Watermelon']

Working with Numeric Data

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

But what if your list contains numeric data?

For instance, 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:

[22, 33, 77, 22, 33]

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 cases, there is no need to place quotes around numeric values):

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

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

print(my_new_list)

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

[99, 33, 77, 99, 33]