Sometimes, while working with Machine Learning, we can encounter None values and we wish to convert them to an empty string for data consistency. This and many other utilities can require a 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 the string for None or an empty string using the or operator and replace the None values with an empty string.
Python3
test_list = ["Geeks", None , "CS", None , None ]
print ("The original list is : " + str (test_list))
conv = lambda i : i or ''
res = [conv(i) for i in test_list]
print ("The list after conversion of None values : " + str (res))
|
Output :
The original list is : ['Geeks', None, 'CS', None, None]
The list after conversion of None values : ['Geeks', '', 'CS', '', '']
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 test 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 an empty string is returned.
Python3
test_list = ["Geeks", None , "CS", None , None ]
print ("The original list is : " + str (test_list))
res = [ str (i or '') for i in test_list]
print ("The list after conversion of None values : " + str (res))
|
Output :
The original list is : ['Geeks', None, 'CS', None, None]
The list after conversion of None values : ['Geeks', '', 'CS', '', '']
Method #3 : Using map()
We can also use the map function to convert None values to empty strings.
Python3
test_list = [ "Geeks" , None , "CS" , None , None ]
print ( "The original list is : " + str (test_list))
res = list ( map ( lambda x: x if x is not None else "", test_list))
print ( "The list after conversion of None values : " + str (res))
|
Output
The original list is : ['Geeks', None, 'CS', None, None]
The list after conversion of None values : ['Geeks', '', 'CS', '', '']
All the above example code has O(n) time complexity, where n is the number of elements in the list and O(n) space complexity.
Method 4: using list comprehension with a ternary operator:
- Printing original list
- Using list comprehension Converting None to an empty string.
- Printing result
Python3
test_list = [ "Geeks" , None , "CS" , None , None ]
print ( "The original list is: " + str (test_list))
res = ["" if i is None else i for i in test_list]
print ( "The list after conversion of None values: " + str (res))
|
Output
The original list is: ['Geeks', None, 'CS', None, None]
The list after conversion of None values: ['Geeks', '', 'CS', '', '']
Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(n), since a new list is created to store the converted values.
Method 5:Using for loop
This program replaces all occurrences of None with an empty string in a given list of strings.
Algorithm:
- Iterate over the elements of the list using a for loop and range() function.
- Check if the current element is None using the is operator.
- If the current element is None, replace it with an empty string.
- Print the modified list to the console.
Python3
lst = [ 'Geeks' , None , 'CS' , None , None ]
for i in range ( len (lst)):
if lst[i] is None :
lst[i] = ''
print ( "The list after conversion of None values :" , lst)
|
Output
The list after conversion of None values : ['Geeks', '', 'CS', '', '']
Time Complexity: O(N)
The time complexity of this algorithm is O(n), where n is the length of the input list, because we only need to iterate over the list once.
Auxiliary Space: O(N)
The space complexity of this algorithm is also O(n), because we modify the input list in-place and do not create any additional data structures.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!