Python List Replace – Simple & Easy

In this tutorial, I will show you a couple of different ways on how to replace items in a list using Python. This is a beginner-friendly tutorial and I will start with an easy Python List Replace method that I really like. So if you want to learn how to replace elements in a list in Python, read on.

I’m currently going through a Python Bootcamp because I want to better understand the fundamentals of programming and for one of the exercises, I had to find a way to replace a list item in Python. I found a couple of different ways on how to do that I want to share the easiest ones, at least in my opinion, with you.

As always when I write tutorials, I approach them with a beginner mindset. I want to break those things down so that everyone is able to understand them. I usually create Cyber Security tutorials and I want to apply the same principles to my programming tutorials. Let me know in the comments below if this was in fact easy to understand for you.

Table of Contents

Example Use-Case

Let’s assume we have a list of different items and we want to replace an element of this list at a specific index.

Our list:

favorite_colors = ["Red", "Green", "Blue"]
Code language: JavaScript (javascript)

As everything changes, so does our taste for colors. We just don’t like Green anymore and we decide to replace it with something more substantial and classy – Black. But how do we do that? How do we replace an element in a list in Python?

I’ll show you a couple of different ways below.

Method 1 – Python List Replace using Index

Probably the easiest way to replace an element in a list in Python is by re-assigning the value at a specific index. To do this with our favorite_colors list, we can simply use the index of Green (1) and replace it with Black.

favorite_colors = ["Red", "Green", "Blue"] favorite_colors[1] = "Black" print(favorite_colors)
Code language: Python (python)

Output

["Red", "Black", "Blue"]
Code language: Bash (bash)

That is a pretty simple and easy-to-understand way of replacing a list item in Python.

Method 2 – Python Replace Element in List using Python enumerate()

Another way of achieving our goal would be to use the enumerate() function.

favorite_colors = ["Red", "Green", "Blue"] for index, color in enumerate(favorite_colors): if color == "Green": favorite_colors[index] = "Black" print(favorite_colors)
Code language: Python (python)

Output:

["Red", "Black", "Blue"]
Code language: Bash (bash)

This way, we utilize a for loop in combination with the enumerate() function to locate the color “Green” and replace it with the color “Black.

Breaking this down for you a bit, the enumerate() function requires three things:

  • A placeholder for the index
  • A placeholder for the item (in our case the color)
  • And the enumerate() function with our list in it’s parenthesis

The loop then runs over each item of our list and checks if the color is in fact “Green”. If that is the case, we use the same principle as in Method 1 to replace that item at the current index with our new color “Black”. The current index of the color “Green is here represented by favorite_colors[index].

That is essentially the same as favorite_colors[1] as in Method 1 above, but that way we don’t need to manually count the index of the color we want to replace, we can use logic and if/else statements to do that for us.

This method is very scalable and my favorite way of doing it.

Conclusion Python List Replace

When it comes to pure replacements, those are the two most common methods you will come across when you want to replace elements in a list in Python. There is are also things like Python List Comprehensions, but that kind of defeats the purpose of replacing items, so I decided to leave that out.

If you want to check out some other programming tutorials, have a look at the programming category.

Tell us what you think!

This site uses Akismet to reduce spam. Learn how your comment data is processed.