Sometimes while working with a lots of data, we can have a problem in which we have data in form of strings list which are values of dictionary keys and we wish to count occurrences of elements starting with character K. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop + startswith()
This is one way in which this task can be performed. In this, we check for each element in dictionary lists using nested loops in brute force and increase the counter.
# Python3 code to demonstrate working of # Element Frequency starting with K in dictionary value List # using loop + startswith() # initializing dictionary test_dict = {1 : ['Gfg', 'is', 'for', 'Geeks'], 2 : ['Gfg', 'is', 'CS', 'God'], 3: ['Gfg', 'best']} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # initializing K K = 'G' # Element Frequency starting with K in dictionary value List # using loop + startswith() res = 0for sub in test_dict.values(): for ele in sub: if ele.startswith(K): res += 1 # printing result print("The element frequency starting with K : " + str(res)) |
The original dictionary is : {1: ['Gfg', 'is', 'for', 'Geeks'], 2: ['Gfg', 'is', 'CS', 'God'], 3: ['Gfg', 'best']}
The element frequency starting with K : 5
Method #2 : Using sum() + startswith()
This is yet another way in which this task can be performed. In this, we perform task of getting
frequency using sum() and generator is used to perform flattening the logic in one line.
# Python3 code to demonstrate working of # Element Frequency starting with K in dictionary value List # using sum() + startswith() # initializing dictionary test_dict = {1 : ['Gfg', 'is', 'for', 'Geeks'], 2 : ['Gfg', 'is', 'CS', 'God'], 3: ['Gfg', 'best']} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # initializing K K = 'G' # Element Frequency starting with K in dictionary value List # using sum() + startswith() res = sum(ele.startswith(K) for ele in [sub for j in test_dict.values() for sub in j]) # printing result print("The element frequency starting with K : " + str(res)) |
The original dictionary is : {1: ['Gfg', 'is', 'for', 'Geeks'], 2: ['Gfg', 'is', 'CS', 'God'], 3: ['Gfg', 'best']}
The element frequency starting with K : 5
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python - Convert Dictionary Value list to Dictionary List
- Python program to find the sum of the value in the dictionary where the key represents the frequency
- Python | Pretty Print a dictionary with dictionary value
- Python - Replace dictionary value from other dictionary
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python - Convert Frequency dictionary to list
- Python - Dictionary List Values Frequency
- Python | Remove Keys from dictionary starting with K
- Python - Sort dictionary by max/min element in value list
- Python - Convert Key-Value list Dictionary to List of Lists
- Python - Convert list to Single Dictionary Key Value list
- Python - Convert list of dictionaries to Dictionary Value list
- Python - Assigning Key values to list elements from Value list Dictionary
- Python - Convert String List to Key-Value List dictionary
- Python program to update a dictionary with the values from a dictionary list
- Python - Extract ith Key's Value of K's Maximum value dictionary
- Python - Frequency of unequal items in Dictionary
- Python - Frequency Grouping Dictionary
- Python - Cost computation using Frequency and Price dictionary
- Python - Factors Frequency Dictionary
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.

