To count all elements in a list of lists in Python, you can use a nested loop to iterate through each element in each sublist and increment a counter for each element. Here is an example:
my_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
count = 0
for sublist in my_list:
for element in sublist:
count += 1
print(count) # Output: 9
In this example, we first define a list of lists called my_list
. We then initialize a counter variable called count
to 0. We then use a nested loop to iterate through each sublist in my_list
and each element in each sublist. For each element, we increment the count
variable by 1. Finally, we print the value of count
, which gives us the total number of elements in my_list
.
+ There are no comments
Add yours