
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Update Python List Element Value
In Python, lists are one of the built-in data structures that are used to store collections of data. The lists are mutable, which means we can modify their elements after they are created.
Updating Python List Elements
You can update single or multiple list elements using the append, insert, extend, remove, and clear functions. In this article, we will discuss how we can update an existing element in the list.
The list is an index-based sequential data structure. We can access the list elements by their 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 starts from 0 to n?1, and the negative index value starts from ?1 to ?n (i.e., from the ending element to the 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)
Following is the output of the above code:
Original list: [100, 'aa', 89, 'foo'] Updated list: [100, 'aa', 21, 'foo']
Example
Let's take the same 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)
Following is the output of the above code:
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 List Elements using Elements
In the previous examples, we have updated the list element values by using the index position of the element. If we don't know the position of the element, we can update a list using elements.
Example
In the following example, we are trying to update the list element "aa" to 200. In this case, 200 will update multiple times if the element "aa" is 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)
Following is the output of the above code:
Original list: [100, 'aa', 89, 'foo'] Updated list: [100, 200, 89, 'foo']
Updating Multiple List Elements at a time
Let's take an example and update multiple elements at a time. Here we can select multiple items from a list using an index range, i.e., start and end index positions.
Example
In here, we are trying to update the values "aa", 89, and "foo" to "A", "B", and "C" by using the list index range.
list_A = [100, 'aa', 89, 'foo'] print('Original list: ', list_A) list_A[1:4] = 'A', 'B', 'C' print("Updated list: ",list_A)
Following is the output of the above code:
Original list: [100, 'aa', 89, 'foo'] Updated list: [100, 'A', 'B', 'C'
Updating List Elements using in-built Methods
In Python, there are some built-in methods to update the list, like the append() method, which is used to add an element at the end of the list. The insert() method is used to add the element at a particular index position of the list. We can also remove the element using the pop(), remove(), and clear() methods.
Example
In the following example, we have updated the list using the append() function:
my_list=[1,2,3,4,5,6] print(my_list) my_list.append(10) #added element using append() method print("Appended List -",my_list) my_list.insert(6,100) #inserted an element using insert() method print("Inserted List -",my_list) my_list.pop() #removed a last element using pop() method print("Popped List -",my_list)
Following is the output of the above code:
[1, 2, 3, 4, 5, 6] Appended List - [1, 2, 3, 4, 5, 6, 10] Inserted List - [1, 2, 3, 4, 5, 6, 100, 10] Popped List - [1, 2, 3, 4, 5, 6, 100]