Search code examples
pythonlistdecimalstring-conversion

How to convert string to decimal in this list of lists?


I'm trying to convert the strings in this list of lists into decimal form:

ask = [['31188.28758860', 2], ['31183.48445986', 0.14258435], ...]

I tried using elem.strip('"') for elem in ask to remove the quote marks but get "AttributeError: 'list' object has no attribute 'strip'" So I assume I am not accessing the nested level of the strings


Solution

  • [float(elem[0]) for elem in ask]
    

    to just get the first values of your nested list.

    OR

    [[float(elem[0]), elem[1]] for elem in ask]
    

    to get the same structure than before, but with the strings replaced by floats.