Reputation: 39
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
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