Python – Checking the number of internal elements in a 3D string list

Question:

We are working on counting the number of elements in the 3D string list and using that number.

eleli = [[["ele", "ele", "ele", "ele", "ele"], ["ele", "ele", "ele"]], [["ele", "ele", "ele", "ele", "ele"], ["ele", "ele", "ele", "ele"]], [["ele", "ele", "ele", "ele"], ["ele", "ele", "ele", "ele", "ele"]], [["ele", "ele"]]]

print(len(eleli))

>>>4

Is there a simple way to count all string elements in a list?

Asked By: anfwkdrn

||

Answers:

Yes, there is:

print(sum(len(e) for seq in eleli for e in seq))
Answered By: Ecir Hana

Try something like the following:

eleli = [[["ele", "ele", "ele", "ele", "ele"], ["ele", "ele", "ele"]], [["ele", "ele", "ele", "ele", "ele"], ["ele", "ele", "ele", "ele"]], [["ele", "ele", "ele", "ele"], ["ele", "ele", "ele", "ele", "ele"]], [["ele", "ele"]]]

# Find the length of all primitive elements within the inner lists.

total = 0

for i in eleli:
    for j in i:
        total +=len(j)


print(total)

Answered By: Altaf Shaikh
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.