Python – Element Frequency starting with K in dictionary value List
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
test_dict = { 1 : [ 'Gfg' , 'is' , 'for' , 'Geeks' ], 2 : [ 'Gfg' , 'is' , 'CS' , 'God' ], 3 : [ 'Gfg' , 'best' ]}
print ("The original dictionary is : " + str (test_dict))
K = 'G'
res = 0
for sub in test_dict.values():
for ele in sub:
if ele.startswith(K):
res + = 1
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
test_dict = { 1 : [ 'Gfg' , 'is' , 'for' , 'Geeks' ], 2 : [ 'Gfg' , 'is' , 'CS' , 'God' ], 3 : [ 'Gfg' , 'best' ]}
print ("The original dictionary is : " + str (test_dict))
K = 'G'
res = sum (ele.startswith(K) for ele in [sub for j in test_dict.values() for sub in j])
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
test_dict = { 1 : [ 'Gfg' , 'is' , 'for' , 'Geeks' ], 2 : [ 'Gfg' , 'is' , 'CS' , 'God' ], 3 : [ 'Gfg' , 'best' ]}
print ( "The original dictionary is : " + str (test_dict))
K = 'G'
res = sum ([sub.count(ele) for j in test_dict.values() for sub in j for ele in sub if ele.startswith(K)])
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’.
Method #5 : Using find(),extend(),values() methods
Approach
- Used a for loop+extend(),values() methods to convert nested values list of strings to a single list
- Initialised result variable to 0
- Initiated a for loop to traverse list of strings and check whether each string is starting with K
- If yes increment result variable by 1
- Display result variable at the end of for loop
Python3
test_dict = { 1 : [ 'Gfg' , 'is' , 'for' , 'Geeks' ], 2 : [ 'Gfg' , 'is' , 'CS' , 'God' ], 3 : [ 'Gfg' , 'best' ]}
print ( "The original dictionary is : " + str (test_dict))
K = 'G'
res = 0
x = []
for i in list (test_dict.values()):
x.extend(i)
for i in x:
if i.find(K) = = 0 :
res + = 1
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
Share your thoughts in the comments
Please Login to comment...