Convert String Float to Float List in Python (original) (raw)
Last Updated : 06 Feb, 2025
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 can break it into individual string elements and then apply map() to convert each element into a float. This produces a list of floats derived from the original string.
Python `
s = '1.23 4.56 7.89'
Split the string 's' into a list of substrings and convert each substring to a float using map
f = list(map(float, s.split())) print(f)
`
**Explanation:
- s.split() splits the string s at each space, producing a list of string representations of the float values.
- map(float, …) applies the float function to each element of the list, converting the strings to floats, and list() creates a list from the resulting map object.
Using List Comprehension
We can split a string into individual elements and convert each element to a float in one line using list comprehension, resulting in a concise list of float values from the original string.
Python `
s = '1.23 4.56 7.89' # Define a string 's' containing float values separated by spaces
Use list comprehension to split the string and convert each element to a float
f = [float(x) for x in s.split()] print(f)
`
**Explanation:
- s.split() splits the string s into a list of substrings, each representing a float value, based on spaces.
- The list comprehension [float(x) for x in s.split()] converts each substring into a float and stores the results in the list f.
Using re.split()
re.split() function can be used to split a string based on a regular expression pattern (such as spaces or other delimiters) and then map() or a list comprehension can convert each substring into a float. This method provides greater flexibility in handling different delimiters when splitting the string.
Python `
import re
s = '1.23,4.56;7.89'
Use re.split() to split the string by both commas and semicolons
f = [float(x) for x in re.split('[,;]', s)] print(f)
`
**Explanation:
- re.split(‘[,;]’, s) splits the string s at both commas and semicolons, using the regular expression pattern [ , ; ] to match these delimiters.
- The list comprehension [float(x) for x in re.split(‘[,;]’, s)] converts each substring (split by the delimiters) into a float and stores them in the list f.
Similar Reads
- 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 - List of float to string conversion 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 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 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 Float to digit list We are given a floating-point number and our task is to convert it into a list of its individual digits, ignoring the decimal point. For example, if the input is 45.67, the output should be [4, 5, 6, 7]. Using string manipulationIn this method, the number is converted to a string and then each chara 4 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
- Python - Filter float strings from String list Sometimes, while working with Python list, we can have a problem in which we need to separate the float values from valid strings. But problem arises when float values are in form of strings. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + Exception Handling 8 min read
- Convert Each Item in the List to String using Python Converting each item in a list to a string is a common task when working with data in Python. Whether we're dealing with numbers, booleans or other data types, turning everything into a string can help us format or display data properly. We can do this using various methods like loops, the map() fun 3 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
- Python - Convert Integer Matrix to String Matrix Given a matrix with integer values, convert each element to String. Input : test_list = [[4, 5, 7], [10, 8, 3], [19, 4, 6]] Output : [['4', '5', '7'], ['10', '8', '3'], ['19', '4', '6']] Explanation : All elements of Matrix converted to Strings. Input : test_list = [[4, 5, 7], [10, 8, 3]] Output : [ 6 min read