Q:

Write a Python program to count the number of sublists contain a particular element

0

Write a Python program to count the number of sublists contain a particular element.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

def count_element_in_list(input_list, x): 
    ctr = 0
    for i in range(len(input_list)): 
        if x in input_list[i]: 
            ctr+= 1
          
    return ctr
      

list1 = [[1, 3], [5, 7], [1, 11], [1, 15, 7]] 
print("Original list:")
print(list1)
print("\nCount 1 in the said list:")
print(count_element_in_list(list1, 1)) 
print("\nCount 7 in the said list:")
print(count_element_in_list(list1, 7)) 


list1 = [['A', 'B'], ['A', 'C'], ['A', 'D', 'E'], ['B', 'C', 'D']] 
print("\nOriginal list:")
print(list1)
print("\nCount 'A' in the said list:")
print(count_element_in_list(list1, 'A')) 
print("\nCount 'E' in the said list:")
print(count_element_in_list(list1, 'E'))

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now