Flatten a list of lists containing single strings to a list of ints

You can simply do this:

allyears = [int(i[0]) for i in allyears]

Because all the elements in your allyears is a list which has ony one element, so I get it by i[0]

The error is because ypu can't convert a list to an int


You're very close, you just need to take the first (and only) element of allyears[i] before doing the int conversion:

for i in range(0, len(allyears)): 
    allyears[i] = int(allyears[i][0]) 

Alternatively, you could do this in one line using a list comprehension:

allyears = [int(l[0]) for l in allyears]

Tags:

Python