Skip to content

Python Replace Values in List With Examples Malli Spark By {Examples}

  • by

How to replace values or elements in List list in Python? You can replace a list in python by using many ways, for example, by using the list indexing, list slicing, list comprehension, map(), and lambda function.

In Python, lists are a type of collection data structure that allows you to store multiple elements of different types in a single variable. Each item in a list is called an element, and elements are separated by commas and enclosed within square brackets []. You can replace an item in a list by assigning a new value to the specific index position in the list.

In this article, I will explain different ways to replace a list in Python with examples.

1. Quick Examples of Replacing List Values

If you are in a hurry, below are some quick examples of how to replace a list.

# Quick examples of replace list

# Initialize list
lists = [‘Spark’,’Python’,’Pandas’,’Pyspark’,’Java’,’Hadoop’]

# Example 1: Using list indexing
lists[0] = ‘MongoDB’
lists[1] = ‘Numpy’

# Example 2: Replace list using lambda function
lists = list(map(lambda x: x.replace(‘Hadoop’, ‘MongoDB’), lists))

# Example 3: Replace the list using list comprehension
lists = [x if x != ‘Pandas’ else ‘C++’ for x in lists]

# Example 4: Replace list using list slicing
indx = lists.index(‘Pyspark’)
lists = lists[:indx]+[‘Salesforce’]+lists[indx+1:]

# Example 5: Replacing list using numpy
lists = np.array(lists)
result = np.where(lists == ‘Java’, ‘MongoDB’, lists)

2. Replace the List Using List Indexing

You can replace elements or values in a list by assigning a new value to the list by index in Python. With this approach, you can replace a value of a list at any random index without accessing all elements of the list.

# Initialize list
lists = [‘Spark’,’Python’,’Pandas’,’Pyspark’,’Java’,’Hadoop’]

# Replace using list indexing
lists[0] = ‘MongoDB’
lists[1] = ‘Numpy’
print(lists)

Here, we first initialize a list lists containing six elements. then replace the element at the index 0 (which is ‘Spark‘) with ‘MongoDB. Similarly, replaced the element at the index 1 (which is ‘Python’) with ‘Numpy’.

The above example yields the below output.

# Output:
[‘MongoDB’, ‘Numpy’, ‘Pandas’, ‘Pyspark’, ‘Java’, ‘Hadoop’]

3. Replace List Using map() and Lambda Function

You can use a lambda function along with a map() to replace elements in a list in Python. The map() function applies this lambda function to all elements of the list and returns a new iterator with the transformed elements. To replace value used the replace() function.

# Replace list using lambda function
lists = list(map(lambda x: x.replace(‘Hadoop’, ‘MongoDB’), lists))
print(lists)

Here, initialized a list named lists with six-string elements and then replace any occurrence of the string ‘Hadoop’ with ‘MongoDB’ using a lambda function and the map() function. Note that map() return an iterator hence, convert this iterator back to a list using the list() function.

Yields below output. You can see that the element ‘Hadoop’ has been replaced with ‘MongoDB’ in the lists list.

# Output:
[‘Spark’, ‘Python’, ‘Pandas’, ‘Pyspark’, ‘Java’, ‘MongoDB’]

4. Replace a List Using List Comprehension

Alternatively, you can also use list comprehension to replace an element in a list. List comprehension allows us to create lists in a concise and expressive way.

# Replace the list using list comprehension
lists = [x if x != ‘Pandas’ else ‘C++’ for x in lists]
print(lists)

Here, list comprehension creates a new list by iterating over each element x in the list. For each element, it checks whether x is equal to “Pandas” and if so, it replaces it with “C++”. Otherwise, it keeps the original value of x.

The above example yields the below output.

# Output:
[‘Spark’, ‘Python’, ‘C++’, ‘Pyspark’, ‘Java’, ‘Hadoop’]

5. Replacing the list Using List Slicing

You can use list slicing to replace a specific value in a list with another value. In this approach, First, we get the index of the element to replace and then use slicing to split the list and then merge it back by adding a replaced element at the index position.

# Replace list using list slicing
indx = lists.index(‘Pyspark’)
lists = lists[:indx]+[‘Salesforce’]+lists[indx+1:]
print(lists)

Here, first use the index of the list ‘Pyspark’ using the index() method and store it in the variable index. You can then use list slicing to replace the item at this index with a new value, which is ‘Salesforce‘.

The above example yields the below output.

# Output:
[‘Spark’, ‘Python’, ‘Pandas’, ‘Salesforce’, ‘Java’, ‘Hadoop’]

6. Using For Loop

You can replace a list using a for loop in Python. In the below example, we use a for loop to iterate over all the elements in the list. Inside the loop, it checks if the current element is equal to ‘Pandas’ or ‘Hadoop’ using if statements. If it is, it replaces the element with ‘Hyperion’ or ‘C++’ respectively, using the lists[i] = new_value syntax. Finally, it prints the updated list which shows the changes.

# Initialize list
lists = [‘Spark’,’Python’,’Pandas’,’Pyspark’,’Java’,’Hadoop’]

# Replace list in python
# Using for loop
for i in range(len(lists)):
# Replace ‘Pandas’ with ‘Hyperion’
if lists[i] == ‘Pandas’:
lists[i] = ‘Hyperion’

# Replace ‘Hadoop’ with ‘C++’
if lists[i] == ‘Hadoop’:
lists[i] = ‘C++’
print(lists)

Yields below output.

# Output:
[‘Spark’, ‘Python’, ‘Hyperion’, ‘Pyspark’, ‘Java’, ‘C++’]

7. Using While Loop

Similarly, you can also use while loop in Python to replace a value in a List with a new value. For example, you first initialize a list lists containing six elements. It then uses a while loop to iterate over all the elements in the list. Inside the loop, it checks if the current element is equal to ‘Spark‘ or ‘Python’ using if statements. If it is, it replaces the element with ‘MongoDB‘ or ‘Numpy’ respectively.

# Replace list using while loop
i = 0
while i < len(lists):
# Replace ‘Spark’ with ‘MongoDB’
if lists[i] == ‘Spark’:
lists[i] = ‘MongoDB’

# Replace ‘Python’ with ‘Numpy’
if lists[i] == ‘Python’:
lists[i] = ‘Numpy’

i += 1
print(lists)

Yields below output.

# Output:
[‘MongoDB’, ‘Numpy’, ‘Pandas’, ‘Pyspark’, ‘Java’, ‘Hadoop’]

8. Replacing the list Using NumPy

Finally, you can also use NumPy to replace an element in a list. For example, you want to replace the element “Java” with “MongoDB”. First, you convert the list into a NumPy array using np.array(lists). Then, we use the np.where() function to create a new array called result.

import numpy as np

# Initialize list
lists = [‘Spark’,’Python’,’Pandas’,’Pyspark’,’Java’,’Hadoop’]

# Replacing list using numpy
lists = np.array(lists)
result = np.where(lists == ‘Java’, ‘MongoDB’, lists)
print(result)

Yields below output.

# Output:
[‘Spark’ ‘Python’ ‘Pandas’ ‘Pyspark’ ‘MongoDB’ ‘Hadoop’]

Conclusion

In this article, you have learned replacing a list can be done by using Python for loop, while loop, list indexing, list slicing, list comprehension, map(), and lambda function with examples. Based on your need you can use any approach explained above.

Happy Learning !!

 How to replace values or elements in List list in Python? You can replace a list in python by using many ways, for example, by using the list indexing, list slicing, list comprehension, map(), and lambda function. In Python, lists are a type of collection data structure that allows you to store multiple elements of  Read More Python, Python Tutorial, Python List Examples 

Leave a Reply

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