Element Occurrence in Dictionary of List Values - Python Last Updated : 08 Feb, 2025 Comments Improve Suggest changes Like Article Like Report We are having a dictionary of list we need to find occurrence of all elements. For example, d = {'a': [1, 2, 3, 1], 'b': [3, 4, 1], 'c': [1, 5, 6]} we need to count the occurrence of all elements in dictionary list so that resultant output should be {'a': 2, 'b': 1, 'c': 1}.Using a Dictionary ComprehensionWe can use a dictionary comprehension to count occurrences of an element in each list value of dictionary. This approach iterates over dictionary items and applies count() method to determine frequency of target element in each list Python d = {'a': [1, 2, 3, 1], 'b': [3, 4, 1], 'c': [1, 5, 6]} ele = 1 # Target element to count # Dictionary comprehension to count occurrences of 'ele' in each list o = {k: v.count(ele) for k, v in d.items()} print(o) # Output: {'a': 2, 'b': 1, 'c': 1} Output{'a': 2, 'b': 1, 'c': 1} Explanation:Dictionary comprehension iterates over each key-value pair in d and uses v.count(ele) to count occurrences of ele in list.Result is a new dictionary where each key maps to count of ele in its corresponding list.Using collections.CounterWe can use collections.Counter to count element occurrences in each list of dictionary efficiently. By applying Counter to each list we retrieve frequency counts and access target element’s count using .get() defaulting to zero if absent. Python from collections import Counter d = {'a': [1, 2, 3, 1], 'b': [3, 4, 1], 'c': [1, 5, 6]} ele = 1 # Target element to count # Use Counter to count occurrences of 'ele' in each list o = {k: Counter(v)[ele] for k, v in d.items()} print(o) Output{'a': 2, 'b': 1, 'c': 1} Explanation:Counter(v) creates a frequency dictionary for each list mapping elements to their counts.Counter(v)[ele] retrieves count of ele defaulting to zero if ele is not present.Using a LoopWe iterate over dictionary using a for loop and count target element in each list using the count() method results are stored in a new dictionary mapping each key to its corresponding count. Python d = {'a': [1, 2, 3, 1], 'b': [3, 4, 1], 'c': [1, 5, 6]} ele = 1 o = {} for key, values in d.items(): # Count occurrences of 'ele' in the list and store in 'o' o[key] = values.count(ele) print(o) Output{'a': 2, 'b': 1, 'c': 1} Explanation:For loop iterates over each dictionary item using count(ele) to determine occurrences of ele in list.Resulting counts are stored in a new dictionary mapping each key to frequency of ele in its corresponding list. Comment More infoAdvertise with us Next Article Element Occurrence in Dictionary of List Values - Python manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Python dictionary-programs Practice Tags : python Similar Reads Get Index of Values in Python Dictionary Dictionary values are lists and we might need to determine the position (or index) of each element within those lists. Since dictionaries themselves are unordered (prior to Python 3.7) or ordered based on insertion order (in Python 3.7+), the concept of "index" applies to the valuesâspecifically whe 3 min read Get List of Values From Dictionary - Python We are given a dictionary and our task is to extract all the values from it and store them in a list. For example, if the dictionary is d = {'a': 1, 'b': 2, 'c': 3}, then the output would be [1, 2, 3].Using dict.values()We can use dict.values() along with the list() function to get the list. Here, t 2 min read Python | Test if element is dictionary value Sometimes, while working with a Python dictionary, we have a specific use case in which we just need to find if a particular value is present in the dictionary as it's any key's value. This can have use cases in any field of programming one can think of. Let's discuss certain ways in which this prob 4 min read Get Python Dictionary Values as List - Python We are given a dictionary where the values are lists and our task is to retrieve all the values as a single flattened list. For example, given the dictionary: d = {"a": [1, 2], "b": [3, 4], "c": [5]} the expected output is: [1, 2, 3, 4, 5]Using itertools.chain()itertools.chain() function efficiently 2 min read Python - Print dictionary of list values In this article, we will explore various ways on How to Print Dictionary in Python of list values. A dictionary of list values means a dictionary contains values as a list of dictionaries in Python. Example: {'key1': [{'key1': value,......,'key n': value}........{'key1': value,......,'key n': value} 4 min read Like