Sometimes, while working with Python, we can have a problem in which we need to get occurrence of 1 element in another. But as a modification of this, we can have a problem in which we need to count the occurrence of all elements of 1 list in another. Lets discuss certain ways in which this task can be performed.
Method #1 : Using nested loops
This is one of the way in which this task can be performed. This is brute force way in which this task can be performed. In this, we iterate one list and then target list, if element match, we increase the counter.
# Python3 code to demonstrate # Sum of each List element occurrence in another # using nested loops # Initializing lists test_list1 = [1, 3, 4, 5, 1, 4, 4, 6, 7] test_list2 = [4, 6, 1] # printing original lists print("The original list 1 is : " + str(test_list1)) print("The original list 2 is : " + str(test_list2)) # Sum of each List element occurrence in another # using nested loops res = 0for ele in test_list2: for ele1 in test_list1: if ele1 == ele: res = res + 1 # printing result print ("The occurrence count : " + str(res)) |
The original list 1 is : [1, 3, 4, 5, 1, 4, 4, 6, 7] The original list 2 is : [4, 6, 1] The occurrence count : 6
Method #2 : Using sum() + count()
The combination of above methods can be used to perform this particular task. This is one liner alternative to the above method. In this counting is done using count() and accumulation using sum().
# Python3 code to demonstrate # Sum of each List element occurrence in another # using sum() + count() # Initializing lists test_list1 = [1, 3, 4, 5, 1, 4, 4, 6, 7] test_list2 = [4, 6, 1] # printing original lists print("The original list 1 is : " + str(test_list1)) print("The original list 2 is : " + str(test_list2)) # Sum of each List element occurrence in another # using sum() + count() res = sum(test_list1.count(idx) for idx in test_list2) # printing result print ("The occurrence count : " + str(res)) |
The original list 1 is : [1, 3, 4, 5, 1, 4, 4, 6, 7] The original list 2 is : [4, 6, 1] The occurrence count : 6
Recommended Posts:
- Python | Last occurrence of some element in a list
- Python | Element Occurrence in dictionary of list values
- Python | K occurrence element Test
- Python | Check element for range occurrence
- Python | Sort tuple based on occurrence of first element
- Python - Occurrence counter in List of Records
- Python | Consecutive Maximum Occurrence in list
- Python | Count occurrence of all elements of list in a tuple
- Python | Subtract two list elements if element in first list is greater
- Python | Replace elements in second list with index of same element in first list
- Python | Count tuples occurrence in list of tuples
- Python | Indices list of matching element from other list
- Python | Insert Nth element to Kth element in other list
- Python | Slicing list from Kth element to last element
- Python | Move one list element to another list
- Python | Every Kth element in list
- Python | Three element sum in list
- Python | How to get the last element of list
- Python | Insert after every Nth element in a list
- Python | Remove given element from the list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

