I have a list within a list, and want to change all values to integers. I have accomplished this, but suppose I had the user specify how many lists within lists they will have. How can I convert all of these values into integers as well?
I have the following code which currently takes any list, or any list within a list, and converts all values to integers. I can see how I can expand on this to more lists within lists, but that may be unnecessary, or possibly not enough. And of course, this seems very slow and takes a lot of code.
for i in l:
index = l.index(i)
l[index] = list(map(int, l[index]))
My list is l
, and each list within l
is i
. I take the index of whatever i is, and convert all values within it to an integer. How can I create a function/loop that takes in how many lists within lists there are, and convert them all to integers?
For example on this input:
l = [['6', '5'], '7', ['88', '99', '1']]
The above code will return:
[[6, 5], 7, [88, 99, 1]]
However, if I have:
l = [['6', '7'], ['6', ['7', '8']]]
I receive the error:
"TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'"
I understand that this is because I am only converting lists nested in lists, not lists nested in lists nested in lists, so it thinks that I am trying to convert a whole list into an integer.
Long story short, how can I build a function that can convert any number of nested lists into integers?