I want to write a function that takes a list of numbers and lists, which again can contain numbers and lists, and so on..., and returns the overall amount of numbers that are somewhere in the list.
Example: [1,[[[0,2],7,3],5,[1,2]],3]
contains 9 numbers in it.
This is my code so far:
test=[1,[[[0,2],7,3],5,[1,2]],3]
def flatten(mylist):
counter = 0
for i in range(len(mylist)):
if type(mylist[i]) == int:
counter += 1
if type(mylist[i]) == list:
[item for sublist in mylist[i] for item in sublist]
counter += 1
return counter
I think I need to recursivley flatten the sublists. But I get the error: TypeError: 'int' object is not iterable