How to update the list element in Python? You can use the assignment operator (=) to assign a new value to an existing element in a list based on its index or use several Python methods to update elements in the list.
If you don’t know the index of the list element and you want to update it, you can use for loop to iterate over each element, and update them based on their values. In this article, I will explain examples of different approaches to updating single/multiple elements in a list.
1. Quick Examples of Updating List Elements
If you are in a hurry, below are some quick examples of updating list elements.
# Quick examples of updating the list
# Example 1: Update the list element
# at index 2 to 35
mylist = [10, 20, 30, 40, 50]
mylist[2] = 35
# Example 2: Update the multiple elements in a list
# Using slicing
mylist = [10, 20, 30, 40, 50]
mylist[1:3] = [15, 20, 25]
# Example 3: Update the list elements
# in the specified range
mylist = [10, 20, 30, 40, 50]
new_values = [35, 45]
start_index = 1
end_index = 3
mylist[start_index:end_index] = new_values
# Example 4: Update the list
# Using for loop
mylist = [10, 20, 30, 40, 50]
for i, item in enumerate(mylist):
if item == 30:
mylist[i] = 78;
# Example 5: Update the list Element
# With full range
mylist = [10, 20, 30, 40, 50]
for item in range(0, len(mylist)):
if mylist[item] == 20:
mylist[item] = 29;
# Example 6: Update the list Element
# Within a limited range
mylist = [10, 30, 30, 40, 50]
for item in range(0, len(mylist)-3):
if mylist[item] == 30:
mylist[item] = 15;
# Example 7: Using the List Methods to Update the List
# Update the list using update() method
mylist = [10, 30, 30, 40, 50]
mylist.append(70)
# Example 8: Update the list using extend() method
mylist.extend([20, 30])
# Example 9: Update the list using pop() method
mylist.pop()
# Example 10: Update the list using remove() method
mylist.remove(20)
2. Update Existing Elements in the List
To update directly an existing element in a list, with a new value, you can use the assignment(=
) operator with the specified index. For example, you can update the element at the index position of 2
in a mylist
by assigning the value 35
to mylist[2]
. After updating, the list becomes [10, 20, 35, 40, 50]
.
# Initialize list
mylist = [10, 20, 30, 40, 50]
print("Original list: ",mylist)
# Update the list element
# at index position 2 with 35
mylist[2] = 35
print("After updating list is:", mylist)
Yields below output.
3. Update Multiple Elements in a List
You can also update multiple elements in the list using the slicing and assignment operator. For example, you have a list mylist
of elements [10, 20, 30, 40, 50]
and you want to update the elements in the range from index 1
to index 2
(excluding the element at the index 3
) with the new values [15, 20, 25]
. Using this syntax mylist[1:3] = [15, 20, 25]
, you can replace the elements in the specified range with the new values.
# Initialize list
mylist = [10, 20, 30, 40, 50]
print("Original list: ",mylist)
# Update multiple elements in a list
# Using slicing
mylist[1:3] = [15, 20, 25]
print("After updating the list is:", mylist)
Yields below output.
In another way, you can use slicing and assignment to update multiple elements in a list with new values.
In the below example, you have a list mylist
of elements [10, 20, 30, 40, 50]
and you want to update the elements in the range from index 1
to index 2
(excluding the element at the index 3
) with the new values [35, 45]
. By using mylist[start_index:end_index]=new_values
, you replace the elements in the specified range with the new values.
# Initialize list
mylist = [10, 20, 30, 40, 50]
print("Original list: ",mylist)
# Update the elements
# In the specified range
new_values = [35, 45]
start_index = 1
end_index = 3
mylist[start_index:end_index] = new_values
print("After updating the list is:", mylist)
# Output:
# Original list: [10, 20, 30, 40, 50]
# After updating the list is: [10, 35, 45, 40, 50]
4. Update List Elements Using For Loop
You can also update the list using a for loop and the enumerate() function. For example, you have a list mylist
of elements [10, 20, 30, 40, 50]
and you want to update the list by replacing any occurrence of the value 30
with the value 78
. By using a for loop
and the enumerate()
function, you can iterate over each item and its corresponding index in the list. Inside the loop, you can use the if statement to check if the current item is equal to 30
. If it is, you update the item at that index to 78
using mylist[i] = 78
.
Python enumerate()
allows you to access both the index and item in each iteration of the loop, making it convenient to update elements based on specific conditions.
# Initialize list
mylist = [10, 20, 30, 40, 50]
print("Original list: ",mylist)
# Update the list
# Using for loop
for i, item in enumerate(mylist):
if item == 30:
mylist[i] = 78;
print("After updating the list is:", mylist)
# Output:
# Original list: [10, 20, 30, 40, 50]
# After updating the list is: [10, 20, 78, 40, 50]
5. Update List Element With Full Range
You can also update the list elements within the full range using a for loop with the range() function. For example, you have a list mylist
of elements [10, 20, 30, 40, 50]
and you want to update the list by replacing any occurrence of the value 20
with the value 29
. By using a for loop and the range()
function, you iterate over the indices of the list. Inside the loop, you check if the item at the current index is equal to 20
using an if statement. If it is, you update the item at that index to 29
using mylist[item] = 29
.
# Initialize list
mylist = [10, 20, 30, 40, 50]
print("Original list: ",mylist)
# Update list element
# with full range
for item in range(0, len(mylist)):
if mylist[item] == 20:
mylist[item] = 29;
print("After updating the list is:", mylist)
# Output:
# Original list: [10, 20, 30, 40, 50]
# After updating the list is: [10, 29, 30, 40, 50]
6. Update List Element within Limited Range
You can also update the list elements within a limited range using a for loop and the range()
function. For example, you have a list mylist
of elements [10, 30, 30, 40, 50]
and you want to update the list by replacing any occurrence of the value 30
within a limited range (up to the third last element) with the value 15
.
By using a for loop and the range()
function, you can iterate over the indices of the list up to len(mylist)-3
. This ensures that the loop only iterates up to the third last element. Inside the loop, you check if the item at the current index is equal to 30
using an if statement. If it is, you update the item at that index to 15
using mylist[item]=15
.
# Initialize list
mylist = [10, 30, 30, 40, 50]
print("Original list: ",mylist)
# Update list Element
# Within limited range
for item in range(0, len(mylist)-3):
if mylist[item] == 30:
mylist[item] = 15;
print("After updating the list is:", mylist)
# Output:
# Original list: [10, 30, 30, 40, 50]
# After updating the list is: [10, 15, 30, 40, 50]
7. Using List Methods to Update the List
Python list allows various methods to update the list elements in different ways.
7.1 Using update()
To add an item or element to a list (which can be an integer, list, etc.), you can use the list.append() function. For instance, if you have a list named(my_list
) and you want to append an element (70
) to the end, simply use the append() function. Here’s an example:
# Initialize list
mylist = [10, 30, 30, 40, 50]
print("Original list: ",mylist)
# Appending elements
# To the end of the list
mylist.append(70)
print("After updating the list is:", mylist)
# Output:
# Original list: [10, 30, 30, 40, 50]
# After updating the list is: [10, 30, 30, 40, 50, 70]
7.2 Using extend()
You can use a list.extend() method to update the list by appending the elements from another iterable. Let’s apply the extend() method over the list to add a collection of elements at the end of the list.
# Initialize list
mylist = [10, 20, 30, 40, 50]
print("Original list: ",mylist)
# Update the list using extend() method
mylist.extend([20, 30])
print("After updating the list is:", mylist)
# Output:
# Original list: [10, 20, 30, 40, 50]
# After updating the list is: [10, 20, 30, 40, 50, 20, 30]
7.3 Using pop()
You can use a list.pop() method to update the list by removing and returning the last element (or an element at a specified index). Let’s apply the pop() method over the list to remove the last element or an element at a specified index.
# Initialize list
mylist = [10, 20, 30, 40, 50]
print("Original list: ",mylist)
# Update the list using pop() method
mylist.pop()
print("After updating the list is:", mylist)
# Output:
# Original list: [10, 20, 30, 40, 50]
# After updating the list is: [10, 20, 30, 40]
7.4 Using remove()
You can use a list.remove() method to update the list by removing the first occurrence of the specified element. Let’s apply the remove() method over the list to remove the first occurrence of a specified element.
# Initialize list
mylist = [10, 20, 30, 20, 50]
print("Original list: ",mylist)
# Update the list using remove() method
mylist.remove(20)
print("After updating the list is:", mylist)
# Output:
# Original list: [10, 20, 30, 20, 50]
# After updating the list is: [10, 30, 20, 40]
8. Conclusion
In this article, I have explained how to update single/multiple elements of a list by using several approaches in Python. Also explained some of the list methods and using those methods how we can update the list with examples.
Happy Learning !!
Related Articles
- Python – range() Inclusive Values
- Reverse a Range in Python with Examples
- Python range() with float values