Skip to content
Related Articles

Related Articles

Python Program to get all unique keys from a List of Dictionaries

View Discussion
Improve Article
Save Article
  • Last Updated : 12 Aug, 2022
View Discussion
Improve Article
Save Article

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)

Output:

['is', 'ria', 'my', 'name']

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:

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)

Output:

['ria', 'my', 'name', 'is']

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)

Output

['ria', 'name', 'is', 'my']

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!