Open In App

Python – Element Frequency starting with K in dictionary value List

Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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 = 0
for 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))


Output : 

The original dictionary is : {1: ['Gfg', 'is', 'for', 'Geeks'], 2: ['Gfg', 'is', 'CS', 'God'], 3: ['Gfg', 'best']}
The element frequency starting with K : 5

Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list

  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




# 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))


Output : 

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 #3 : Using list comprehension + count()

Python3




#Python3 code to demonstrate working of
#Element Frequency starting with K in dictionary value List
#using list comprehension + count()
#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 list comprehension + count()
res = sum([sub.count(ele) for j in test_dict.values() for sub in j for ele in sub if ele.startswith(K)])
 
#printing result
print("The element frequency starting with K : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original dictionary is : {1: ['Gfg', 'is', 'for', 'Geeks'], 2: ['Gfg', 'is', 'CS', 'God'], 3: ['Gfg', 'best']}
The element frequency starting with K : 5

Time Complexity : O(n^2)
Auxiliary Space : O(n)
Explanation:
Here we are using a nested list comprehension to flatten the list of sublists of values in the dictionary and then counting the number of elements starting with ‘K’.

Method #5 : Using find(),extend(),values() methods

Approach 

  1. Used a for loop+extend(),values() methods to convert nested values list of strings to a single list
  2. Initialised result variable to 0
  3. Initiated a for loop to traverse list of strings and check whether each string is starting with K 
  4. If yes increment result variable by 1
  5. Display result variable at the end of for loop

Python3




# Python3 code to demonstrate working of
# Element Frequency starting with K in dictionary value List
 
# 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
res = 0
x=[]
for i in list(test_dict.values()):
    x.extend(i)
for i in x:
    if i.find(K)==0:
        res+=1
# printing result
print("The element frequency starting with K : " + str(res))


Output

The original dictionary is : {1: ['Gfg', 'is', 'for', 'Geeks'], 2: ['Gfg', 'is', 'CS', 'God'], 3: ['Gfg', 'best']}
The element frequency starting with K : 5

Time Complexity : O(n^2)
Auxiliary Space : O(n)
Explanation:
Here we are using a nested list comprehension to flatten the list of sublists of values in the dictionary and then counting the number of elements starting with ‘K’.



Last Updated : 25 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads