Skip to content

How to Update List Element in Python? Malli Spark By {Examples}

  • by

How to update the list element in Python? You can use the assignment operator (=) to assign a new value to an existing element 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 you want to update, you can iterate over the list using a loop, compare the elements, and update them accordingly. 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 multiple elements in a list
# Using slicing
mylist = [10, 20, 30, 40, 50]
mylist[1:3] = [15, 20, 25]

# Example 3: Update the 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 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 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 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: Update the list by appending
# elements to the end of the list
mylist = [10, 30, 30, 40, 50]
mylist.append(70)

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.

2. 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. Appending Elements to the End of the List

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]

8. Conclusion

In this article, I have explained how to update single/multiple elements within a Python list by using several approaches.

Happy Learning !!

 How to update the list element in Python? You can use the assignment operator (=) to assign a new value to an existing element 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 you want to update, you can  Read More Python, Python Tutorial, Pending review by admin, Python List Examples 

Leave a Reply

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