Last Updated on August 6, 2021 by Roshan Parihar
In this tutorial, learn how to update list element using Python. The short answer is: Use the index position within the square bracket ([]
) and assign the new element to change any element of the 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 on 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.
1 2 3 |
myList = ["one", "two", 37, "four", 51]; myList[2] = "three"; print(myList); |
Output
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 strings 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.
Change Required List Items Using For Loop in Python
To update the required list element, you have to use 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.
1 2 3 4 5 |
myList = ["one", "two", 37, "four", 51]; for i, item in enumerate(myList): if item == 37: myList[i] = 48; print(myList); |
Output
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.
Replace Old Item With New in List Within Range Using Python
You can change or update the 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 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 elements contain the same integer value. The full range may change all the matching elements
1 2 3 4 5 |
myList = ["one", "two", 37, "four", 51, 37]; for item in range(0, len(myList)): if myList[item] == 37: myList[item] = 29; print(myList); |
Output
The above example contains an element ’29’ that is two times present in the list. The full range of loop methods changes 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.
1 2 3 4 5 |
myList = ["one", "two", 37, "four", 51, 37]; for item in range(0, len(myList)-3): if myList[item] == 37: myList[item] = 29; print(myList); |
Output
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.
DOWNLOAD Free PYTHON CHEAT SHEET
FAQS on How to Update List Element in Python
Q1. How to Update List in Python?
Answer: To update a list in Python, you have to use the List variable followed by the index operator ([]
). Within that operator, you have to pass the index position of the element that you want to update. After that, assign a new value to it to update the list in Python.
Q2. Can You Update a Value in List in Python?
Answer: Yes, you can update a value in the list in Python using the index operator ([]
). You need to just pass a position of the element as the argument of the index operator ([]
). Now, assign a new value to update with the existing value in Python.
You May Also Like to Read
- How to copy list elements to another variable using Python
- Find the index of the given element of the list Using Python
- Find the size of a list item using Python
I hope you like this post on how to update the list element using Python.
Reference
Stackoverflow Discussion on Modifying List Items in Python