Open In App

Python | Empty String to None Conversion

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Machine Learning, we can encounter empty strings and we wish to convert to the None for data consistency. This and many other utilities can require the solution to this problem. Let’s discuss certain ways in which this problem can be solved. 

Method #1 : Using lambda This task can be performed using the lambda function. In this we check for string for None or empty string using the or operator and replace the empty string with None. 

Python3




# Python3 code to demonstrate working of
# Empty String to None Conversion
# Using lambda
 
# initializing list of strings
test_list = ["Geeks", '', "CS", '', '']
 
# printing original list
print("The original list is : " + str(test_list))
 
# using lambda
# Empty String to None Conversion
conv = lambda i : i or None
res = [conv(i) for i in test_list]
 
# printing result
print("The list after conversion of Empty Strings : " + str(res))


Output : 

The original list is : ['Geeks', '', 'CS', '', '']
The list after conversion of Empty Strings : ['Geeks', None, 'CS', None, None]

Time Complexity: O(n) where n is the number of elements in the string list. The lambda is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the string list.

  Method #2 : Using str() Simply the str function can be used to perform this particular task because, None also evaluates to a “False” value and hence will not be selected and rather a string converted false which evaluates to empty string is returned. 

Python3




# Python3 code to demonstrate working of
# Empty String to None Conversion
# Using str()
 
# initializing list of strings
test_list = ["Geeks", '', "CS", '', '']
 
# printing original list
print("The original list is : " + str(test_list))
 
# using str()
# Empty String to None Conversion
res = [str(i or None) for i in test_list]
 
# printing result
print("The list after conversion of Empty Strings : " + str(res))


Output : 

The original list is : ['Geeks', '', 'CS', '', '']
The list after conversion of Empty Strings : ['Geeks', None, 'CS', None, None]

Time Complexity: O(n) where n is the number of elements in the string list. The str() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the test list.

 Method #3 : Using  for loop

method to convert empty strings to None in a list is to iterate through each element of the list, and if the element is an empty string, replace it with None using a conditional statement.

Python3




# input list
lst = ['Geeks', '', 'CS', '', '']
 
# iterate through each element and replace empty strings with None
for i in range(len(lst)):
    if lst[i] == '':
        lst[i] = None
 
# output the updated list
print("The list after conversion of Empty Strings : ", lst)


Output

The list after conversion of Empty Strings :  ['Geeks', None, 'CS', None, None]

Time complexity: O(n)

Auxiliary Space: O(n)

Method #4: Using a list comprehension and the replace() method:

Algorithm:

1.Initialize the list with some values, including empty strings.
2.Use the replace() method in a list comprehension to replace empty strings with None in the list.
3.Print the updated list.

Python3




# initializing list of strings
test_list = ["Geeks", '', "CS", '', '']
# printing original list
print("The original list is : " + str(test_list))
 
# using replace() to convert empty strings to None
test_list = [val if val != '' else None for val in test_list]
 
# printing the updated list
print("The list after conversion of Empty Strings : "+str(test_list))
#This code is contributed by Jyothi pinjala


Output

The original list is : ['Geeks', '', 'CS', '', '']
The list after conversion of Empty Strings : ['Geeks', None, 'CS', None, None]

The time complexity : O(nm), where n is the length of the input list and m is the maximum length of a string in the list. This is because the replace() method has a time complexity of O(m), and we apply it to each element in the list, which takes O(n) time.

The auxiliary space : O(n), as we create a new list of the same size as the input list to store the updated values. The replace() method itself does not take any additional space.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads