Python list replace: How to replace string, integer in List

0
697
Python List Replace - How to Replace String, Integer in List

String in Python is an immutable Data type. Replacing a string with another string is a regular operation in any development. The string replace() method in Python will help us replace a string with a particular string in the list of Strings.

Python list replace

To replace an element in the Python list, use the list comprehension. To replace a string in a Python list, use the combination of list comprehension + string replace().

Example

Let’s see the following code example.

# app.py

data = ['Python', '5', 'Golang', '4', 'PHP', '3']

# printing data list
print("The original list : " + str(data))

# using list comprehension + replace()
# Replace substring in list of strings
res = [item.replace('4', '5') for item in data]

# print result
print("The list after substring replacement : " + str(res))

Output

The original list : ['Python', '5', 'Golang', '4', 'PHP', '3']
The list after substring replacement : ['Python', '5', 'Golang', '5', 'PHP', '3']

We defined a Python list and then used the list comprehension to replace 4 with 5, return the new list, and then printed the list.

Python String replace() method replaces a specified phrase with another.

Let’s see another example in which we replace the Golang string to Go.

data = ['Python', '5', 'Golang', '4', 'PHP', '3']

# printing data list
print("The original list : " + str(data))

# using list comprehension + replace()
# Replace substring in list of strings
res = [item.replace('Golang', 'Go') for item in data]

# print result
print("The list after string replacement : " + str(res))

Output

The original list : ['Python', '5', 'Golang', '4', 'PHP', '3']
The list after string replacement : ['Python', '5', 'Go', '4', 'PHP', '3']

If nothing particular is specified, then all occurrences of the specified phrase will be replaced.

List replace Using map() + lambda + replace()

Use the combination of Python map(), lambda function, and string replace() functions to replace a string with another string in the Python list.

See the following code.

data = ['Python', '5', 'Golang', '4', 'PHP', '3']

# printing data list
print("The original list : " + str(data))

# using map() + lambda + replace()
# Replace string in list of strings
res = list(map(lambda st: str.replace(st, "PHP", ".NET"), data))

# print result
print("The list after string replacement : " + str(res))

Output

The original list : ['Python', '5', 'Golang', '4', 'PHP', '3']
The list after string replacement : ['Python', '5', 'Golang', '4', '.NET', '3']

In the above code, we have defined the list and then used the map() function to replace PHP string to .Net String and convert that iterator into a list using the list() function.

Replace integer in Python List

To replace an integer with an integer in a Python list, use the ternary operator and list comprehension.

See the following code.

data = [19, 11, 23, 18, 46, 23]

# printing data list
print("The original list : " + str(data))

# using list comprehension + ternary operator
# Replace integer in list of integers
res = [21 if item == 23 else item for item in data]

# print result
print("The list after integer replacement : " + str(res))

Output

The original list : [19, 11, 23, 18, 46, 23]
The list after integer replacement : [19, 11, 21, 18, 46, 21]

In the above example, we have defined a list, and we need to replace 23 integer with 21 integer.

If it finds multiple integers, it will replace all the numbers with specified values. So keep in mind that. To do that using list comprehension, we have to use a ternary operator in the list.

So we have written if you find 23 on the list, then replace it with 21 and return the list.

That is it for this tutorial, and thanks for taking it.

See also

Python list sort()

Python list extend()

Python list append()

Python list contains

Python list copy()

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.