Search code examples
pythonliststring-conversion

Convert string values to list python


I'm trying to figure out an easy way to take a string in a line from a file that is being read using readline(). Specifically, there are multiple integer values which is where I am running into issues:

10 20 30 

I would like the values above to be converted into a list of separate integers:

[10, 20, 30] 

The values would then be summed within a separate class. I'm certain there is something simple I could do here, but I'm just drawing a blank. Thanks in advance!

Here's more context as to what I am trying to do. I'm passing the integers into an updateMany method in my class:

vals = infile.readline() 
a.updateMany(int(vals.split())

updateMany() takes the list and sums them, adding them to a list variable local to the class.


Solution

  • For example:

    thelist = [int(s) for s in thestring.split()]