I have the following test code in Python:
lst1 = ["1", "2", "3"]
lst2 = ["4", "5", "6"]
lst3 = ["7", "8", "9"]
numbers = [lst1, lst2, lst3]
for lst in numbers:
lst = list(map(int, lst))
print (numbers)
What I'm trying to do is convert all the string variables in lst1
, lst2
and lst3
, which are all sublists inside the list numbers
, into integers. I'm using the map()
function to achieve this by iterating through each list and converting the list, but somehow, it's not working.
Now, I tried a different method using the following code:
numbers = [list(map(int, lst)) for lst in numbers]
For some reason, this works. Does anyone know why the former code doesn't work and the latter does? I'd just like to wrap my head around it so that I fully understand why it doesn't work.
Thanks!