In this tutorial, learn how to update list element using Python. Use the index position and assign the new element to change any element of List. You can change the element of the list or item of the list with the methods given here.

The elements of the list can contain a string, integers, and objects. You can update these items of the list with the desired position or location using the index. If you don’t know the index of the element, you can perform a loop to compare and change or update the element of the list.

Update List Element Using Index Position in Python

To update the list element using index position. You have to use the list variable within the position of the index. See the example given below to learn how to use this method.

Output

[‘one’, ‘two’, ‘three’, ‘four’, 51]

The above example contains the 5 elements in the list. In these elements, 3 are a string and 2 are integers. However, after you replace or update the elements. It’s showing 4 string and 1 integer in the output.

However, this method will not work if you don’t know the element index position. In that case, you have to use the method given in the below section. So, read further to learn this method and change the element in Python.

Updating Required List Element Using For Loop in Python

To update the required list element, you have to use the Python for loop. In the loop, you have to use the if condition to compare and change only the required element.

Check the below-given example and change the required element as per your requirement.

Output

[‘one’, ‘two’, 48, ‘four’, 51]

The above example first finds the required element by comparing each element. After it gets the required element, the code updates the elements of the list with the newly assigned element.

How to Update List Element Within Range in Python

You can change or update list element within full range or limited range. The full range may replace all the matching elements of the list. It may replace more than the one element of the list.

To change the element within a certain range. You have to limit the range and perform the loop to the list elements. Check each example given below to learn the method.

Update List Element with Full Range
To change the list element with the full range, you have to use the Python range() and pass the argument as given below.

You have to notice one thing here, the 2nd and last index element contain the same integer value. The full range may change all the matching elements

Output

[‘one’, ‘two’, 29, ‘four’, 51, 29]

The above example contains element 29 for two times. The full range of loop method change all these matching elements of the list. If you want to change the element only within a certain range, read further.

Update List Element within Limited range
You can limit the loop iteration using the Python range() function. Use the method given in the example below to apply the limited range.

Output

[‘one’, ‘two’, 29, ‘four’, 51, 37]

The above compares the matching element within the first three elements. After it matches with the element, it changes or updates the element with the new element.

You may also like to read

Hope, you like this post of how to update list element using Python. If you have any query regarding the tutorial, please comment below.

Also tell me, which method you are using to change the element with the new element in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *

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