I need to write a loop that traverses the list and prints the length of each element. So far I have this:

``````def countEachElement():
elements= ["spam!",'1',['Brie','Roquefort','Pol le Veq'],['1','2','3']]
i=0
while i < len(elements):
print len(elements[i])
i=i+1``````

But it's not counting each element of the lists inside the list. What should I do?

You can use a recursive auxiliary function to count an element

``````def count(element):
if isinstance(element, str):
return len(element)
else:
return sum(count(x) for x in element)

def countEachElement():
elements= ["spam!",'1',['Brie','Roquefort','Pol le Veq'],['1','2','3']]
return count(elements)

print(countEachElement())

""" my output -->
32
"""``````

Remarks:
1) PLEASE, configure your editor to insert 4 spaces when you hit the tab key instead of a tab character.
2) It's Pont Leveque and not Pol le veq :)

In the same vein, here is some more fun

``````def concat(element):
if isinstance(element, str):
return element
else:
return "".join(concat(x) for x in element)

def recursionFun():
elements= ["spam!",'1',['Brie','Roquefort','Pol le Veq'],['1','2','3']]
result = concat(elements)
print result
print len(result)

recursionFun()

""" my output -->
spam!1BrieRoquefortPol le Veq123
32
"""``````

Perhaps this is not exactly what you want ? What do you expect as output ?

Maybe the poster liked to have list containing integer length instead of the element for every element of the list. Then for line 5 use statement to return list of counts.

What happens or is supposed to happen if the elements are numbers? Maybe len(str(element)) is better to add to if the case of element not having __iter___, not only strings.

``````def count(element):
if isinstance(element, str) or not hasattr(element,'__iter__'):
return len(str(element))
else:
return [count(x) for x in element]

def countEachElement():
elements= ["spam!",1,['Brie','Roquefort','Pol le Veq'],['1',2.123,'3']]
return count(elements)

print(countEachElement())

""" my output -->
[5, 1, [4, 9, 10], [1, 5, 1]]
"""``````

But remember that purpose of Life, Universe and Everything is 42 ;)!

Maybe the poster liked to have list containing integer length instead of the element for every element of the list. Then for line 5 use statement to return list of counts.

What happens or is supposed to happen if the elements are numbers? Maybe len(str(element)) is better to add to if the case of element not having __iter___, not only strings.

``````def count(element):
if isinstance(element, str) or not hasattr(element,'__iter__'):
return len(str(element))
else:
return [count(x) for x in element]

def countEachElement():
elements= ["spam!",1,['Brie','Roquefort','Pol le Veq'],['1',2.123,'3']]
return count(elements)

print(countEachElement())

""" my output -->
[5, 1, [4, 9, 10], [1, 5, 1]]
"""``````

But remember that purpose of Life, Universe and Everything is 42 ;)!

You may want to check `hasattr(element, '__len__')` instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.