Python Program to get all unique keys from a List of Dictionaries
Given a list arr[] consisting of N dictionaries, the task is to find the sum of unique keys from the given list of the dictionary.
Examples:
Input: arr = [{‘my’: 1, ‘name’: 2}, {‘is’: 1, ‘my’: 3}, {‘ria’: 2}]
Output: [‘ria’, ‘my’, ‘is’, ‘name’]
Explanation: The set of unique keys are {“ria”, “my”, “Is”, “name”}.Input: arr = [{‘X’: 100, ‘Y’: 2}, {‘Z’: 1, ‘Z’: 30}, {‘X’: 21}]
Output: [‘Z’, ‘X’, ‘Y’]
Explanation: The set of unique keys are {“X”, “Y”, “Z”}.
Approach using Chain iterable tools: The problem can be solved using set() and keys() methods and chain iterable tools to solve the above problem.
Follow the steps below to solve the problem:
- Traverse all keys of every dictionary using chain iterable tools
- Store the set of keys in a list, say res.
- Print the list res as the required answer.
Below is the implementation of the above approach:
Python3
# Python3 program for the above approach from itertools import chain # Function to print all unique keys # present in a list of dictionaries def UniqueKeys(arr): # Stores the list of unique keys res = list ( set (chain.from_iterable(sub.keys() for sub in arr))) # Print the list print ( str (res)) # Driver Code arr = [{ 'my' : 1 , 'name' : 2 }, { 'is' : 1 , 'my' : 3 }, { 'ria' : 2 }] UniqueKeys(arr) |
['my', 'is', 'name', 'ria']
Time Complexity: O(N * maxm), where maxm denotes the size of the longest dictionary.
Auxiliary Space: O(N * maxm)
Approach using List Comprehension and Dictionary Comprehension: The problem can be solved alternately using set() and keys() method and list comprehension and dictionary comprehension to solve the problem.
Follow the steps below to solve the problem:
- Traverse all the keys of every dictionary using List Comprehension and Dictionary Comprehension and then store the set of keys in a list, say res.
- Print the list res as the required answer.
Below is the implementation of the above approach:
Python3
# Python3 program for the above approach from itertools import chain # Function to print all unique keys # from a list of dictionaries def UniqueKeys(arr): # Stores the list of unique keys res = list ( set (val for dic in arr for val in dic.keys())) # Print the list print ( str (res)) # Driver Code # Input arr = [{ 'my' : 1 , 'name' : 2 }, { 'is' : 1 , 'my' : 3 }, { 'ria' : 2 }] UniqueKeys(arr) |
['is', 'name', 'ria', 'my']
Time Complexity: O(N * maxm), where maxm denotes the size of the longest dictionary.
Auxiliary Space: O(N * maxm)
Approach : Using keys(),extend(),list() and set() methods
Python3
# Python3 program for the above approach arr = [{ 'my' : 1 , 'name' : 2 }, { 'is' : 1 , 'my' : 3 }, { 'ria' : 2 }] new_list = [] for i in arr: new_list.extend( list (i.keys())) new_list = list ( set (new_list)) print (new_list) |
['name', 'my', 'ria', 'is']
Approach : Using append(),Counter(),keys(), and list() methods
Python3
# Python3 program for the above approach from collections import Counter arr = [{ 'my' : 1 , 'name' : 2 }, { 'is' : 1 , 'my' : 3 }, { 'ria' : 2 }] new_list = [] for i in arr: for j in i: new_list.append(j) uniq = Counter(new_list) print ( list (uniq.keys())) |
['my', 'name', 'is', 'ria']
Please Login to comment...