How we can update a Python list element value?


In Python lists are one the built−in data structure which is used to store collections of data. The lists are mutable, which means we can modify its element after it is created. We have some list methods like append, insert, extend, remove, and clear which are used to change the list content by adding, updating, or removing elements from the list object. In this article, we will discuss how we can update an existing element in the list.

Updating the list elements using the index position

The list is an index−based sequential data structure, so that we can access the list elements by its index position known as index values. The index values of the Python lists are represented in two ways: positive indexing and negative indexing.

The positive indexing is started from 0 to n−1, and the negative index value is started from −1 to −n (i.e,ending element to starting element).

Example

We have to change the value of the 3rd element from 89 to 21. For this we accessed the 3rd element from the list using square brackets and the index value of the element (i.e, 2 because we used the positive index).

list_A = [100, 'aa', 89, 'foo']
print('Original list: ', list_A)

# Update value of 3rd element in list
list_A[2] = 21
print("Updated list: ",list_A)

Output

Original list:  [100, 'aa', 89, 'foo']
Updated list:  [100, 'aa', 21, 'foo']

Example

Let’s take the same above example and update the 2nd element “aa” by using the negative index.

list_A = [100, 'aa', 89, 'foo']
print('Original list: ', list_A)
# Update value of 3rd element in list
list_A[-3] = 2022
print("Updated list: ",list_A)

Output

Original list:  [100, 'aa', 89, 'foo']
Updated list:  [100, 2022, 89, 'foo']

The 2nd element of the list is updated from “aa” to 2022 (string type to integer type) by using the negative index value.

Updating elements by the element

In the previous examples we have updated the list element values by using the index position of the element. What if we don’t know the position of the element? Then we can try like below.

Example

In the following example we are trying to update the list elements “aa” to 200. In this case 200 will update multiple times if the element “aa” present in multiple locations.

list_A = [100, 'aa', 89, 'foo']
print('Original list: ', list_A)

for idx, item in enumerate(list_A):
   if 'aa' == item:
       list_A[idx] = 200
print("Updated list: ",list_A)

Output

Original list:  [100, 'aa', 89, 'foo']
Updated list:  [100, 200, 89, 'foo']

Updating multiple element values at a time

Let’s take an example and update the multiple elements at a time. Here we can select multiple items from a list using index range, i.e., start : end index positions.

Example

In here we are trying to update the values “aa”, 89, “foo” to “A”, “B”, “C” by using the list lindex range.

list_A = [100, 'aa', 89, 'foo']
print('Original list: ', list_A)

list_A[1:4] = 'A', 'B', 'C'
print("Updated list: ",list_A)

Output

Original list:  [100, 'aa', 89, 'foo']
Updated list:  [100, 'A', 'B', 'C'

Updated on: 24-Aug-2023

426 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements