Python - Change List Item
Lists in Python are mutable meaning their items can be changed after the list is created. Modifying elements in a list is a common task, whether we're replacing an item at a specific index, updating multiple items at once, or using conditions to modify certain elements. This article explores the different ways to change a list item with practical examples.
Using Indexing
Using Indexing we update a specific item in the list by simply assigning a new value to the desired index.
Example:
a = [10, 20, 30, 40]
# Change the item at index 1
a[1] = 25
print(a)
Output
[10, 25, 30, 40]
Explanation:
- The statement
a[1] = 25modifies the element at index 1 (second position) of the list, replacing20with25.
Let's explore some other methods to change list item
Table of Content
Using Slicing for Multiple Items
Slicing allows changing multiple items at once by specifying a range of indices. This method is useful for batch updates and has a time complexity of O(n) where n is the size of the slice.
Example:
a = [10, 20, 30, 40, 50]
# Replace items at indices 1 and 2
a[1:3] = [21, 31]
print(a)
Output
[10, 21, 31, 40, 50]
Explanation:
- Using slicing,
a[1:3], we target the elements at indices 1 and 2 (i.e.,20and30). This slice specifies the range of items to be replaced. - The statement
a[1:3] = [21, 31]replaces the selected slice with the new elements[21, 31].
Using List Comprehension
List comprehension is another simple way to update items in a list based on conditions. Here, we Iterate over the list and apply a condition or transformation and construct a new list with the updated values.
Example:
a = [10, 15, 20, 25]
# Doubling even numbers
a = [x * 2 if x % 2 == 0 else x for x in a]
print(a)
Output
[20, 15, 40, 25]
Explanation:
- Using a list comprehension,
[x * 2 if x % 2 == 0 else x for x in a], we iterate over each element in the list. For each element: - If the element is even (
x % 2 == 0), we double it (x * 2). - If the element is odd, we leave it unchanged.
Using a Loop with enumerate()
If we want to modify elements in place a for loop with enumerate() allows us to iterate through the list while keeping track of indices.
Example:
a = [10, 15, 20, 25]
#Adding 5 to odd numbers
for i, x in enumerate(a):
if x % 2 != 0:
a[i] += 5
print(a)
Output
[10, 20, 20, 30]
Explanation:
- Using a
forloop withenumerate, we iterate through each index and value in the list. For each element: If the element is odd (x % 2 != 0), we add 5 to its value (a[i] += 5). - After the loop completes, the updated list
abecomes[10, 20, 20, 30].
Using map()
The map() function can be used for functional-style updates by applying a function to each element. We use map() to apply the function and convert the result back to a list.
Example:
# Example: Incrementing all numbers by 1
a = [10, 20, 30]
a = list(map(lambda x: x + 1, a))
print(a)
Output
[11, 21, 31]
Explanation:
mapfunction is combined with alambdafunction to increment each element in the list by 1:lambda x: x + 1defines an anonymous function that adds 1 to its input (x).mapfunction applies this lambda to every element in the lista.