I have a function that takes a list of floats and returns the same list of floats except they have exactly 4 digits after the decimal, as strings
Currently it is simply
for i in range(len(floats)): floats[i] = "%.4f" %floats[i]
Which does the job.
But can I do this using the map function (since http://wiki.python.org/moin/PythonSpeed/PerformanceTips says using map is faster)
I call this function several thousand times and from the profile results it is one of the functions that takes up more time.
mapis only faster if you use built-in functions (or at least it was when I read about it once ;)). You could use list comprehension though:Note that
mapand list comprehension will create a new list, whereas theforloop won’t. This might be relevant.Depending on the rest of your application, if possible, you should format the numbers when you add them to the list.