Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

list

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?

like image 321
anfwkdrn Avatar asked Jun 13 '26 08:06

anfwkdrn


2 Answers

Yes, there is:

print(sum(len(e) for seq in eleli for e in seq))
like image 57
Ecir Hana Avatar answered Jun 14 '26 23:06

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)

like image 23
Altaf Shaikh Avatar answered Jun 14 '26 21:06

Altaf Shaikh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!