Python | Convert None to empty string
Sometimes, while working with Machine Learning, we can encounter None values and we wish to convert to the empty string 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 None values with empty string.
# Python3 code to demonstrate working of # Converting None to empty string # Using lambda # initializing list of strings test_list = ["Geeks", None, "CS", None, None] # printing original list print("The original list is : " + str(test_list)) # using lambda # Converting None to empty string conv = lambda i : i or '' res = [conv(i) for i in test_list] # printing result print("The list after conversion of None values : " + str(res)) |
The original list is : ['Geeks', None, 'CS', None, None] The list after conversion of None values : ['Geeks', '', 'CS', '', '']
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 code to demonstrate working of # Converting None to empty string # Using str() # initializing list of strings test_list = ["Geeks", None, "CS", None, None] # printing original list print("The original list is : " + str(test_list)) # using str() # Converting None to empty string res = [str(i or '') for i in test_list] # printing result print("The list after conversion of None values : " + str(res)) |
The original list is : ['Geeks', None, 'CS', None, None] The list after conversion of None values : ['Geeks', '', 'CS', '', '']
Recommended Posts:
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | First Non-Empty String in list
- Python program to check if string is empty or not
- Python | Splitting list on empty string
- Python | Convert String to Binary
- Python | Convert String to bytes
- Python | Program to convert a tuple to a string
- Python | Convert dictionary object into string
- Python program to convert hex string to decimal
- Python program to convert a list to string
- Python | Program to convert String to a List
- Python | Convert a list of characters into a string
- Python | Convert Bytearray to Hexadecimal String
- Python | Ways to convert list of ASCII value to string
- Python | Convert byteString key:value pair of dictionary to String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



