Skip to content
Fix Code Error

In Python, how do I convert all of the items in a list to floats?

March 13, 2021 by Code Error
Posted By: snk

I have a script which reads a text file, pulls decimal numbers out of it as strings and places them into a list.

So I have this list:

['0.49', '0.54', '0.54', '0.54', '0.54', '0.54', '0.55', '0.54', '0.54',  '0.54', 
 '0.55', '0.55', '0.55', '0.54', '0.55', '0.55', '0.54', '0.55', '0.55', '0.54']

How do I convert each of the values in the list from a string to a float?

I have tried:

for item in list:
    float(item)

But this doesn’t seem to work for me.

Solution

[float(i) for i in lst]

to be precise, it creates a new list with float values. Unlike the map approach it will work in py3k.

Answered By: Anonymous

Related Articles

  • Combining items using XSLT Transform
  • Python Linked List
  • Can't install via pip because of egg_info error
  • How can I format a decimal to always show 2 decimal places?
  • Python: Float to Decimal conversion and subsequent…
  • C# group by a list contains another class
  • In Python, why when I pass a list to function, I can…
  • How do I include certain conditions in SQL Count
  • Equivalent code from python apply function to…
  • How can I count the depth in a list of lists?
  • What's the difference between eval, exec, and compile?
  • How do I merge two dictionaries in a single…
  • Fix top buttons on scroll of list below
  • Check if all elements in a list are identical
  • How to truncate float values?
  • Design DFA accepting binary strings divisible by a…
  • What are the undocumented features and limitations…
  • In CSS Flexbox, why are there no "justify-items" and…
  • Most effective way to parse JSON Objects
  • How do you split a list into evenly sized chunks?
  • Conversion of System.Array to List
  • Is it possible to apply CSS to half of a character?
  • Python 3 Float Decimal Points/Precision
  • How exactly does the python any() function work?
  • How to update multiple key and values in dictionary?
  • Centering in CSS Grid
  • Assigning a variable NaN in python without numpy
  • How to use html template with vue.js
  • Float vs Decimal in ActiveRecord
  • Remove multiple keys and values with its nested…
  • How to generate a random string of a fixed length in Go?
  • Regular expression to match numbers with or without…
  • How to convert number to words in java
  • insert tables in dataframe with years from 2000 to…
  • return two dimensional array from function with…
  • C++ template,typename and operator
  • Finding all possible combinations of numbers to…
  • Active tab issue on page load HTML
  • How to update Python?
  • Sorting 1 million 8-decimal-digit numbers with 1 MB of RAM
  • I'm trying to use a stored procedure or function…
  • How to append multiple values to a list in Python
  • How to save all repeated loop results in R in a dataframe
  • Align input points with multiple variable lengths
  • Pandas pivot_table: filter on aggregate function
  • Change column type in pandas
  • Difference between numeric, float and decimal in SQL Server
  • Smart way to truncate long strings
  • How can I pass a wct test while rearranging children…
  • Fastest way to iterate over all the chars in a String
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Returning the product of a list
  • C compile error: Id returned 1 exit status
  • Install pip in docker
  • How to format a phone number in a textfield
  • Split polyline by different lengths based on output
  • Display number always with 2 decimal places in
  • Round a double to 2 decimal places
  • Polymer 1.0: Sorting iron-list
  • Is floating point math broken?
  • IsNumeric gives unexpected results in excel/vba for…
  • How to add 'load more' functionality to items on a…
  • Best way to replace multiple characters in a string?
  • Reference - What does this regex mean?
  • Convert array to nested JSON object - Angular Material tree
  • How to filter a RecyclerView with a SearchView
  • Python data structure sort list alphabetically
  • What does the CSS rule "clear: both" do?
  • React google map is not updating
  • How to convert an XML file to nice pandas dataframe?
  • Filtering or removing array objects from the DTO…
  • Difference between decimal, float and double in .NET?
  • JavaScript gives NaN error on the page but variable…
  • Relative imports for the billionth time
  • What represents a double in sql server?
  • Limiting double to 3 decimal places
  • How to show title in hover - css / jquery
  • Solving Eight-queens in scheme
  • Float to String format specifier
  • Usage of __slots__?
  • Is it possible to have multiple statements in a…
  • Why doesn't the height of a container element…
  • Getting the closest string match
  • Are dictionaries ordered in Python 3.6+?
  • Python: how to assign a name of a list variable to a class
  • How to round the double value to 2 decimal points?
  • How to prevent scrolling the whole page?
  • SQL find sum of entries by date including previous date
  • ember.js: using routes, templates and outlets to…
  • How to make vuetify navigation drawer to close group…
  • Using CTE in Oracle SQL (ORA-00923: FROM keyword not…
  • What does the clearfix class do in css?
  • Runtime impact of writing to a file
  • What is your most productive shortcut with Vim?
  • After a little scroll, the sticky navbar just is not…
  • Detect whether a Python string is a number or a letter
  • Iterable and iterator
  • Backbone.Marionette - Collection within…
  • Fastest way to list all primes below N
  • Animation of each element separately in the v -for…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Is there a float input type in HTML5?

Next Post:

sudo: npm: command not found

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error