0

How to get number of elements in list of lists?

L = ['a,b,c,d','e,f,g,h,i','j,k,l',]

I want to find how many elements in the first list in L.

a,b,c,d = 4 elements
e,f,g,h,i = 5 elements
j,k,l = 3 elements

1
  • That's not a list of lists, that's a list of strings. A list of lists would be [[a,b,c,d],[e,f,g,h,i],[j,k,l]] and would have a different answer (which would be len(L[0])). Commented Jul 19, 2015 at 23:45

1 Answer 1

1

For each string in L you could use the str.count method to count the number of commas and add one:

In [277]: L = ['a,b,c,d','e,f,g,h,i','j,k,l',]

In [278]: [item.count(',')+1 for item in L]
Out[278]: [4, 5, 3]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.