Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Python | List of float to string conversion

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Sometimes, while working with Python list, we can have a problem in which we have to transform the list elements to string. This is easier in case of integral list, as they can be joined easily with join(), and their contents can be displayed. But the case with floats is different, there are additional undesired spaces among it’s values that can cause it’s unsuccess. Let’s discuss a ways in which this error situation can be handled. 

Method #1 : Using list comprehension + join() + str() This task can be performed using combination of above functions. In this, we firstly convert each element of list i.e float point number to string and then join the resultant strings using the join(). 

Python3




# Python3 code to demonstrate working of
# List of float to string conversion
# using list comprehension + str() + join()
 
# initialize list
test_list = [5.8, 9.6, 10.2, 45.3, 6.0]
 
# printing original list
print("The original list is : " + str(test_list))
 
# List of float to string conversion
# using list comprehension + str() + join()
res = " ".join([str(i) for i in test_list])
 
# printing result
print("Conversion of float list to string : " + str(res))

Output

The original list is : [5.8, 9.6, 10.2, 45.3, 6.0]
Conversion of float list to string : 5.8 9.6 10.2 45.3 6.0

Time Complexity: O(n) where n is the number of elements in the list “test_list”. list comprehension + join() + str() performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list

Method #2 : Using join() + map() + str() The root method is similar by using the combination of join() + str(), but important function which helps to perform this task is map(). This first converts each element to string and then constructs the master string. 

Python3




# Python3 code to demonstrate working of
# List of float to string conversion
# using join() + map() + str()
 
# initialize list
test_list = [5.8, 9.6, 10.2, 45.3, 6.0]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Convert float list to string
# using join() + map() + str()
res1 = " ".join(str(test_list))
 
# List of float to string conversion
# using join() + map() + str()
res2 = " ".join(map(str, test_list))
 
# printing result
print("Conversion using join + str : " + str(res1))
print("Conversion using join + str + map : " + str(res2))

Output

The original list is : [5.8, 9.6, 10.2, 45.3, 6.0]
Conversion using join + str : [ 5 . 8 ,   9 . 6 ,   1 0 . 2 ,   4 5 . 3 ,   6 . 0 ]
Conversion using join + str + map : 5.8 9.6 10.2 45.3 6.0

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list. 

Method #3: Using format() and for loop – This method uses a for loop to iterate through the list and the format() function to convert each element of the list to a string.

Python3




# Python3 code to demonstrate working of
# List of float to string conversion
# using for loop and format()
 
# initialize list
test_list = [5.8, 9.6, 10.2, 45.3, 6.0]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Convert float list to string
# using for loop and format()
res = ""
for i in test_list:
    res=res+("{:}".format(i))+" "
 
# printing result
print("Conversion of float list to string : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy

Output

The original list is : [5.8, 9.6, 10.2, 45.3, 6.0]
Conversion of float list to string : 5.8 9.6 10.2 45.3 6.0 

This method is similar to the previous method, but it uses a for loop to iterate through the list. It allows more control over the iteration process. The time and space complexity is O(n) as it iterates through the list once.

Method #4 : Using for loop and str() function:

Python3




test_list = [5.8, 9.6, 10.2, 45.3, 6.0]
# printing original list
print("The original list is : " + str(test_list))
result = ''
for item in test_list:
    result += str(item) + ' '
# printing result
print("Conversion of float list to string : " + str(result))
#This code is contributed by Jyothi pinjala

Output

The original list is : [5.8, 9.6, 10.2, 45.3, 6.0]
Conversion of float list to string : 5.8 9.6 10.2 45.3 6.0 

Time Complexity: O(N)
Auxiliary Space: O(N)

Method #5: Using the map() function and lambda function

In this method, we use the map() function with a lambda function to convert each float value in the list to a string value. Then, we use the join() function to join these string values together with a space separator.

Python3




# Python3 code to demonstrate working of
# List of float to string conversion
# using map() + lambda + join()
 
# initialize list
test_list = [5.8, 9.6, 10.2, 45.3, 6.0]
 
# printing original list
print("The original list is : " + str(test_list))
 
# List of float to string conversion
# using map() + lambda + join()
res = " ".join(map(lambda x: str(x), test_list))
 
# printing result
print("Conversion of float list to string : " + str(res))

Output

The original list is : [5.8, 9.6, 10.2, 45.3, 6.0]
Conversion of float list to string : 5.8 9.6 10.2 45.3 6.0

Time Complexity: The time complexity of this method is O(n), where n is the length of the input list.
Auxiliary Space: The auxiliary space used by this method is O(n), where n is the length of the input list.

Method #6: Using a list comprehension and the join() function

This method uses a list comprehension to iterate over the elements of the input list and convert each element to a string. The resulting list of strings is then joined together with spaces using the join() function.

Python3




test_list = [5.8, 9.6, 10.2, 45.3, 6.0]
 
res = " ".join([str(i) for i in test_list])
 
print("Conversion of float list to string : " + str(res))

Output

Conversion of float list to string : 5.8 9.6 10.2 45.3 6.0

Time complexity: O(n), where n is the number of elements in the input list. 
Auxiliary space: O(n), where n is the number of elements in the input list.


My Personal Notes arrow_drop_up
Last Updated : 06 Apr, 2023
Like Article
Save Article
Similar Reads
Related Tutorials