Python List of float to string conversion (original) (raw)
Last Updated : 28 Jan, 2025
When working with lists of floats in Python, we may often need to convert the elements of the list from float to string format. **For example, if we have a list of floating-point numbers like [1.23, 4.56, 7.89], converting them to strings allows us to perform string-specific operations or output them neatly. Let’s explore different methods to perform this conversion.
Using map()
map() function provides a very efficient way to convert all elements of a list. It applies the str() function to each element of the list.
Python `
a = [1.23, 4.56, 7.89]
Convert using map
b = list(map(str, a))
print(b)
`
Output
['1.23', '4.56', '7.89']
**Explanation:
- map() applies the str() function to each element of the list a.
- The result is then converted back into a list using list().
- This method is concise and efficient for large lists.
Let’s explore some more ways and see how we can convert list of float to string in Python.
Table of Content
List Comprehension
List comprehension is a Pythonic way to iterate over the list and convert each element to a string.
Python `
a = [1.23, 4.56, 7.89]
Convert using list comprehension
b = [str(i) for i in a]
print(b)
`
Output
['1.23', '4.56', '7.89']
**Explanation:
- List comprehension iterates through the list a and applies str(i) to each element.
- The result is a new list b where all float elements are converted to strings.
- This method is easy to understand and is preferred when you want more control over the conversion process.
Using for Loop
Using a for loop provides a more explicit way of converting elements in a list. This method is less concise but is useful when we want more customization.
Python `
a = [1.23, 4.56, 7.89]
Convert using for loop
b = [] for i in a: b.append(str(i))
print(b)
`
Output
['1.23', '4.56', '7.89']
**Explanation:
- A for loop iterates through each element of the list a.
- The str(i) is applied inside the loop, and the results are appended to the new list b.
Using join() with map()
If we need to convert the list of floats to a single string where each element is separated by a specific delimiter, we can use map() along with str.join().
Python `
a = [1.23, 4.56, 7.89]
Convert and join elements as a string
b = ', '.join(map(str, a))
print(b)
`
**Explanation:
- map(str, a) converts each element of the list to a string.
- join() concatenates the string elements of the list, separating them by the delimiter , (comma and space).
- This method is useful when you want to output the list as a single formatted string.
Using numpy
If the list is a numpy array, we can use numpy.vectorize() to perform the conversion efficiently.
Python `
import numpy as np
a = np.array([1.23, 4.56, 7.89])
Convert using numpy.vectorize
b = np.vectorize(str)(a)
print(b)
`
Output
['1.23' '4.56' '7.89']
**Explanation:
- numpy.vectorize() applies str() to each element in the numpy array.
- This method is efficient when working with numpy arrays, and the result is a numpy array of strings.
- This approach is mainly useful for those already working within the numpy ecosystem.
Similar Reads
- Convert String Float to Float List in Python We are given a string float we need to convert that to float of list. For example, s = '1.23 4.56 7.89' we are given a list a we need to convert this to float list so that resultant output should be [1.23, 4.56, 7.89]. Using split() and map()By using split() on a string containing float numbers, we 3 min read
- Convert Float String List to Float Values-Python The task of converting a list of float strings to float values in Python involves changing the elements of the list, which are originally represented as strings, into their corresponding float data type. For example, given a list a = ['87.6', '454.6', '9.34', '23', '12.3'], the goal is to convert ea 3 min read
- Python | Convert Joint Float string to Numbers Sometimes, while working with Legacy languages, we can have certain problems. One such can be working with FORTRAN which can give text output (without spaces, which are required) '12.4567.23' . In this, there are actually two floating point separate numbers but concatenated. We can have problem in w 5 min read
- Python | Convert List of String List to String List Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + generator expression + join() + isd 6 min read
- Convert list of strings to list of tuples in Python Sometimes we deal with different types of data types and we require to inter-convert from one data type to another hence interconversion is always a useful tool to have knowledge. This article deals with the converse case. Let's discuss certain ways in which this can be done in Python. Method 1: Con 5 min read
- Python - Summation of float string list Sometimes, while working with Python list, we can have a problem in which we need to find summation in list. But sometimes, we don’t have a natural number but a floating-point number in string format. This problem can occur while working with data, both in web development and Data Science domain. Le 7 min read
- Python - Convert List of Integers to a List of Strings We are given a list of integers and our task is to convert each integer into its string representation. For example, if we have a list like [1, 2, 3] then the output should be ['1', '2', '3']. In Python, there are multiple ways to do this efficiently, some of them are: using functions like map(), re 3 min read
- Convert JSON to string - Python Data is transmitted across platforms using API calls. Data is mostly retrieved in JSON format. We can convert the obtained JSON data into String data for the ease of storing and working with it. Python provides built-in support for working with JSON through the json module. We can convert JSON data 2 min read
- Python | Convert string enclosed list to list Given a list enclosed within a string (or quotes), write a Python program to convert the given string to list type. Examples: Input : "[0, 2, 9, 4, 8]" Output : [0, 2, 9, 4, 8] Input : "['x', 'y', 'z']" Output : ['x', 'y', 'z'] Approach #1: Python eval() The eval() method parses the expression passe 5 min read
- Python | Convert String to tuple list Sometimes, while working with Python strings, we can have a problem in which we receive a tuple, list in the comma-separated string format, and have to convert to the tuple list. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + split() + replace() This is a br 5 min read