How to Replace an Element in a List in Python

To replace an element in a Python list, you can directly assign a new value to the specific index of the element you want to change. Lists in Python are mutable, allowing modifications through index-based assignment or methods like list comprehension and the replace() function for strings inside lists. Below are multiple ways to replace elements in a list.


Examples

1. Replacing an Element Using Indexing

We can replace a specific element in a list using its index by directly assigning a new value to it.

</>
Copy
# Creating a list of fruits
fruits = ["apple", "banana", "cherry", "orange"]

# Replacing "banana" with "grape"
fruits[1] = "grape"

# Printing the updated list
print("Updated List:", fruits)

In this example:

  • The list fruits initially contains four elements: ["apple", "banana", "cherry", "orange"].
  • We access the second element (index 1) and assign "grape" to replace "banana".
  • The modified list is then printed.

Output:

Updated List: ['apple', 'grape', 'cherry', 'orange']

2. Replacing Multiple Elements Using Slicing

We can replace multiple elements at once using slicing.

</>
Copy
# Creating a list of numbers
numbers = [10, 20, 30, 40, 50]

# Replacing elements at index 1 to 3
numbers[1:4] = [25, 35, 45]

# Printing the updated list
print("Updated List:", numbers)

Explanation:

  • The list numbers starts with [10, 20, 30, 40, 50].
  • Using slicing numbers[1:4], we target elements at index 1 to 3 (values [20, 30, 40]).
  • We replace them with [25, 35, 45].
  • The final list updates accordingly.

Output:

Updated List: [10, 25, 35, 45, 50]

3. Replacing an Element Based on a Condition

We can replace an element in a list dynamically based on a condition.

</>
Copy
# Creating a list of words
words = ["hello", "world", "python", "world"]

# Replacing "world" with "universe"
words = ["universe" if word == "world" else word for word in words]

# Printing the updated list
print("Updated List:", words)

Explanation:

  • The list words initially contains ["hello", "world", "python", "world"].
  • We use list comprehension to iterate through the list.
  • Each occurrence of "world" is replaced with "universe".
  • The final list contains the updated values.

Output:

Updated List: ['hello', 'universe', 'python', 'universe']

4. Using map() to Replace Elements

The map() function allows applying a transformation to all elements in a list.

</>
Copy
# Creating a list of numbers
numbers = [1, 2, 3, 4, 5]

# Replacing number 3 with 30 using map
numbers = list(map(lambda x: 30 if x == 3 else x, numbers))

# Printing the updated list
print("Updated List:", numbers)

Explanation:

  • The original list is [1, 2, 3, 4, 5].
  • Using map() with a lambda function, we replace 3 with 30.
  • The result is converted back into a list.

Output:

Updated List: [1, 2, 30, 4, 5]

Conclusion

Python provides multiple ways to replace elements in a list:

  1. Using Indexing: Directly assigning a new value at a specific index.
  2. Using Slicing: Replacing multiple elements at once.
  3. Using a Condition: List comprehension allows conditional replacement.
  4. Using map(): Functional programming approach for replacement.