Hey, I'm on the rookie side, to Python, and Pygame and new to this community, and I'm confused if this could be possible; I want to change the data type of a list, in Python, from strings to integer (they have to be integer, because I need to sort them numerically afterwards).

This is what I have:

fil2 = open('scrs.dat', 'r')
reads = fil2.read()
splits = reads.split('=')

That is what it outputs, but of course the data type is string; the function of this code is to read data which is wrote previously to the file so to store high scores for my game. And I'd want to sort them in numerical order, so is there any method of changing each value to an integer in the list?

5 Years
Discussion Span
Last Post by Ice_Occultism

There is plenty of ways, most recomendable is to use list comprehension:

splits = [int(n) for n in splits]

If you only want to sort them numerically, simplest is to use int as key for sorted-function.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.