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'
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]
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With