Python - Change List Item
Last Updated :
24 Dec, 2024
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:
Python
a = [10, 20, 30, 40]
# Change the item at index 1
a[1] = 25
print(a)
Explanation:
- The statement
a[1] = 25
modifies the element at index 1 (second position) of the list, replacing 20
with 25
.
Let's explore some other methods to change list item
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:
Python
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., 20
and 30
). 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:
Python
a = [10, 15, 20, 25]
# Doubling even numbers
a = [x * 2 if x % 2 == 0 else x for x in a]
print(a)
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:
Python
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)
Explanation:
- Using a
for
loop with enumerate
, 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
a
becomes [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:
Python
# Example: Incrementing all numbers by 1
a = [10, 20, 30]
a = list(map(lambda x: x + 1, a))
print(a)
Explanation:
map
function is combined with a lambda
function to increment each element in the list by 1:lambda x: x + 1
defines an anonymous function that adds 1 to its input (x
).map
function applies this lambda to every element in the list a
.
Similar Reads
Access List Items in Python Accessing elements of a list is a common operation and can be done using different techniques. Below, we explore these methods in order of efficiency and their use cases. Indexing is the simplest and most direct way to access specific items in a list. Every item in a list has an index starting from
2 min read
Extending a list in Python In Python, a list is one of the most widely used data structures for storing multiple items in a single variable. Often, we need to extend a list by adding one or more elements, either from another list or other iterable objects. Python provides several ways to achieve this. In this article, we will
2 min read
Python List of Lists A list of lists in Python is a collection where each item is another list, allowing for multi-dimensional data storage. We access elements using two indices: one for the outer list and one for the inner list. In this article, we will explain the concept of Lists of Lists in Python, including various
3 min read
Are Lists Mutable in Python? Yes, lists are mutable in Python. This means that once a list is created, we can modify it by adding, removing or changing elements without creating a new list.Let's explore some examples that demonstrate how lists can be modified in Python:Changing Elements in a ListSince lists are mutable, you can
3 min read
Python Lists In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s
6 min read