I am trying to convert all elements of the small lists in the big list to integers, so it should look like this:
current list:
list = [['1','2','3'],['8','6','8'],['2','9','3'],['2','5','7'],['5','4','1'],['0','8','7']]
for e in list:
for i in e:
i = int(i)
new list:
list = [[1,2,3],[8,6,8],[2,9,3],[2,5,7],[5,4,1],[0,8,7]]
Could anyone tell me why doesn't this work and show me a method that does work? Thanks!
You can use a nested list comprehension:
converted = [[int(num) for num in sub] for sub in lst]
I also renamed list
to lst
, because list
is the name of the list type and not recommended to use for variable names.
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