list of lists to list of ints python

germanjke :

i have list with lists of strings looks like

allyears
#[['1916'], ['1919'], ['1922'], ['1912'], ['1924'], ['1920']]

i need to have output like this:

#[1916, 1919, 1922, 1912, 1924, 1920]

have been try this:

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

but i have error

>>> TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
Binh :

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=397635&siteId=1