# Replace Element at Certain Index in List in Python (Example)

In this tutorial, you will learn how to replace an element at a specific index in a list in Python. There are situations where you may want to update or modify a particular element in a list without changing the rest of the elements. We will explore different examples to demonstrate this process.

Letâ€™s get started!

## Initializing a Sample List

Firstly, letâ€™s initialize a sample list to work with:

# Initializing a sample list
my_list = [10, 20, 30, 40, 50]

This creates a list named my_list containing five elements.

## Replace an Element at a Certain Index

To replace an element at a specific index in the list, you can directly assign a new value to that index using the assignment operator =. Hereâ€™s an example:

# Replace an element at index 2 with a new value
my_list[2] = 35

print(my_list)
# [10, 20, 35, 40, 50]

In this example, we replaced the element at index 2 (which is 30) with the value of 35. After the replacement, the updated list became [10, 20, 35, 40, 50].

You can also replace multiple elements at different indices by repeating this process for each index. We see that in the output, the element at index 2 has been replaced with the new value.

## Video, Further Resources & Summary

In this tutorial, we learned how to replace an element at a specific index in a list in Python. By directly assigning a new value to the desired index, we can update the element while keeping the rest of the list intact.

Do you need more explanations on looping through a list of integers in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

For more Python programming tutorials and examples, you can check out the following resources on Statistics Globe:

Now you have the knowledge and techniques to replace an element at a certain index in a list in Python. Happy coding!

This page was created in collaboration with Ã–mer Ekiz. Have a look at Ã–merâ€™s author page to get more information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.

Subscribe to the Statistics Globe Newsletter