# Basic syntax:
# Using list comprehension:
sum([len(elem) for elem in list_of_lists])
# Example usage:
# Say you want to count the number of elements in a nested list like:
nested_list = [ [1, 2, 3, 45, 6, 7],
[22, 33, 44, 55],
[11, 13, 14, 15] ]
sum([len(elem) for elem in nested_list])
--> 14
List = ["Elephant", "Snake", "Penguin"]
print(len(List))
# With the 'len' function Python counts the amount of elements
# in a list (The length of the list).
"""Find all occurrences of element in list"""
# If you simply want find how many times an element appears
# use the built-in function count()
def find_occur(lst, item):
return lst.count(item)
# Test -----------------------------------------------------------
print(find_occur([None, None, 1, 2, 3, 4, 5], None)) # 2
# If you wanna find where they occur instead
# - This returns the indices where element is found within a list
def find(lst, item):
return [i for (i, x) in enumerate(lst) if x == item]
# Test Code ------------------------------------------------------
from random import randint, choice
lst = [randint(0, 99) for x in range(10)] # random lst
item = choice(lst) # item to find
found = find(lst, item) # lst of where item is found at
print(f"lst: {lst}",
f"item: {item}",
f"found: {found}",
sep = "
")