How To Replace None Values ​In A List In Python

Replace None values in a List in Python

The value None is a rather annoying value in Python. I don’t want to see it. So today’s topic will be to replace None values ​​in a list in Python. You can use list comprehension, append method, and if statement to do that. Please follow the following article to understand more.

Replace none values ​in a list in Python

Use list comprehension with the ‘is’ operator

Example:

  • The ‘is’ operator with list comprehension finds the value None in the list.
  • Replace None with a string.
valuesList = ['learnshareit', 'web', None, 'site']

# Perform a search and replace the None value.
listReplace = ['18 years old' if i is None else i for i in valuesList]
print('List after replacing the value None', listReplace)

Output:

List after replacing the value None ['learnshareit', 'web', '18 years old', 'site']

You can also replace the value None with an empty string first and then add a non-None value to the empty string.

Example:

valuesList = ['learnshareit', 'web', None, 'site']

# Perform a search and replace for the None value.
listReplace = ['' if i is None else i for i in valuesList]
print('None is replaced with the empty string:', listReplace)

listReplace = ['18 years old' if i is None else i for i in valuesList]
print('List after replacing the value None', listReplace)

Output:

None is replaced with the empty string: ['learnshareit', 'web', '', 'site']
List after replacing the value None ['learnshareit', 'web', '18 years old', 'site']

Use the append method

Syntax:

list.append(element)

Parameters:

  • list: is the original list.
  • element: is the element to add.

The append method adds an element to the end of a list. You can add a number, a list, a string, or a tuple as an element to the end of a list.

Example:

  • Create a list of strings and None.
  • Create a for loop has what it takes: checks for the value of None in the list. If the value is None, replace it with another string and append it to ‘listEmpty’ by append function, and if it’s non-None, obviously add it to ‘listEmpty’ by append function.
valuesList = ['learnshareit', 'web', None, 'site']

# a list that stores a value other than None.
listEmpty = []

# Search, replace value None. Then values other than None are added to 'listEmpty'.
for i in valuesList:
    if i is None: 
        listEmpty.append("18 years old")
    else:
        listEmpty.append(i)

newList = listEmpty 
print('List after replacing the value None', newList)

Output:

List after replacing the value None ['learnshareit', 'web', '18 years old', 'site']

Use the if statement

Example:

  • Create a list of strings and None.
  • If the condition is true, replace the value with None.
  • If False, print to the command line.
valuesList = ['learnshareit', 'web', None, 'site']

# If the condition is true, replace the value with None.
if type(valuesList) is list :
        valuesList[valuesList.index(None)] = '18 years old'
        print('List after replacing the value None:', valuesList)
else:
     print('An error occurred')

Output:

List after replacing the value None: ['learnshareit', 'web', '18 years old', 'site']

Summary

Those are the ways I use to replace None values ​​in a list in Python. It would be best if you used list comprehension. It can do the job quite simple. If you have a better way, comment below to let us know. Thank you for reading.

Maybe you are interested:

Leave a Reply

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