Open In App

Python – Extract Numerical Dictionary values

Last Updated : 14 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python Dictionaries, we can have a problem in which we need to extract only if a particular key index is numeric value from the dictionaries which are in form of strings. This can be desired in applications in which we require to do preprocessing. Let’s discuss certain ways in which this task can be performed.

Input : test_dict = {‘best’: [‘5′, ’35’, ‘geeks’], ‘CS’: [1, 2, 3], ‘Gfg’: [‘124’, ‘4’, ‘8’]} Output : [(‘5’, 1, ‘124’), (’35’, 2, ‘4’)] Input : test_dict = {“Gfg” : [“4”], ‘best’ : [“6”], ‘CS’ : [1]} Output : [(‘6’, 1, ‘4’)]

Method #1 : Using loop + zip() + isdigit() The combination of above functions can be used to perform this task. In this, we check for numeric string using isdigit() and zip to perform the cumulation of keys. 

Python3




# Python3 code to demonstrate working of
# Extract Numerical Dictionary values
# Using loop + zip() + isdigit()
 
# initializing dictionary
test_dict = {"Gfg" : ["34", "45", 'geeks'], 'is' : ["875", None, "15"], 'best' : ["98", 'abc', '12k']}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Extract Numerical Dictionary values
# Using loop + zip() + isdigit()
res = []
for a, b, c in zip(*test_dict.values()):
    if a.isdigit() :
        res.append((a, b, c))
     
# printing result
print("The Numerical values : " + str(res))


Output : 

The original dictionary : {‘Gfg’: [’34’, ’45’, ‘geeks’], ‘best’: [’98’, ‘abc’, ’12k’], ‘is’: [‘875′, None, ’15’]} The Numerical values : [(’34’, ’98’, ‘875’), (’45’, ‘abc’, None)]

Time Complexity: O(n*m) -> N is the number of keys in the dictionary and M is the maximum number of values associated with a key

Auxillary Space: O(1)

  Method #2 : Using list comprehension + zip() + isdigit() The combination of above functions can be used to solve this problem. In this, we perform similar task as above method but as a shorthand using list comprehension. 

Python3




# Python3 code to demonstrate working of
# Extract Numerical Dictionary values
# Using list comprehension + zip() + isdigit()
 
# initializing dictionary
test_dict = {"Gfg" : ["34", "45", 'geeks'], 'is' : ["875", None, "15"], 'best' : ["98", 'abc', '12k']}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Extract Numerical Dictionary values
# Using list comprehension + zip() + isdigit()
res = [(a, b, c) for a, b, c in zip(*test_dict.values()) if a.isdigit()]
     
# printing result
print("The Numerical values : " + str(res))


Output : 

The original dictionary : {‘Gfg’: [’34’, ’45’, ‘geeks’], ‘best’: [’98’, ‘abc’, ’12k’], ‘is’: [‘875′, None, ’15’]} The Numerical values : [(’34’, ’98’, ‘875’), (’45’, ‘abc’, None)]

Time Complexity: O(n*m) -> N is the number of keys in the dictionary and M is the maximum number of values associated with a key

Auxillary Space: O(1)

Sure! Here’s another approach for the same problem, along with step-by-step explanation, time complexity, and auxiliary space complexity:

Method #3: Using dictionary comprehension + str.isdigit() method 

Explanation:

Initialize a dictionary test_dict with three keys, each containing a list of values. Some of the values in the list may contain digits while others may not.
Print the original dictionary using print() function and string concatenation.
Use a dictionary comprehension to iterate over each key-value pair in the test_dict dictionary. For each value list, the list comprehension extracts all the numeric values and creates a new list containing only those values. This new list replaces the old value list for the corresponding key in the dictionary. The str.isdigit() method is used to check if a value is numeric or not.
Create a list numeric_values using a list comprehension to extract the numeric values from each key in the modified test_dict dictionary. The zip() function is used to group the numeric values by their position/index in the original value lists.
Print the final result using print() function and string concatenation.

Python3




# Python3 code to demonstrate working of
# Extract Numerical Dictionary values
# Using dictionary comprehension + str.isdigit()
 
# initializing dictionary
test_dict = {"Gfg": ["34", "45", "geeks"], "is": ["875", None, "15"], "best": ["98", "abc", "12k"]}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Extract Numerical Dictionary values
# Using dictionary comprehension + str.isdigit()
res = {k: [int(i) for i in v if str(i).isdigit()] for k, v in test_dict.items()}
numeric_values = [(a, b, c) for a, b, c in zip(*res.values())]
 
# printing result
print("The Numerical values : " + str(numeric_values))


Output

The original dictionary : {'Gfg': ['34', '45', 'geeks'], 'is': ['875', None, '15'], 'best': ['98', 'abc', '12k']}
The Numerical values : [(34, 875, 98)]

The time complexity of this approach is O(nm), where n is the number of keys in the test_dict dictionary and m is the maximum number of values in any value list.

The auxiliary space complexity of this approach is O(nm), as we are creating a new list of numeric values for each key in the test_dict dictionary.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads