trinidad34
trinidad34

Reputation: 39

Counting elements in a list of lists, and returning the count of each list as a list

I want to count the elements of a list of lists and then have the output be a list of the count of each list. This an example of a list I made.

ListofLists = [[5, 3, 5, 4], ["a", "b", "c"]]

I want to then count the elements of each list and return it as a list as so:

CountofLists = [4, 3]

Any help would be appreciated

Upvotes: 0

Views: 33

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195613

You can combine map() and len() built-in functions:

CountofLists = [*map(len, ListofLists)]
print(CountofLists)

Prints:

[4, 3]

Or:

CountofLists = [len(sublist) for sublist in ListofLists]
print(CountofLists)

Upvotes: 1

Related Questions