Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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

['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:

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

['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

This program takes a list of dictionaries arr where each dictionary contains keys representing words and their frequency counts. It then extracts all the unique keys from all the dictionaries and creates a new list new_list containing only those unique keys. Finally, it prints the new_list.

Follow the below steps to implement the above idea:

  • Create a list of dictionaries named arr. Each dictionary in the list has key-value pairs representing words and their frequency counts.
  • Create an empty list named new_list.
  • Iterate over each dictionary in arr using a for loop. For each dictionary, get a list of keys using the keys() method and convert it to a list using the list() function.
  • Add the resulting list to new_list using the extend() method, which adds each element of the list to new_list one by one.
  • Use the set() function to remove any duplicate elements from new_list.
  • Convert new_list back to a list using the list() function.
  • Print the final new_list containing all unique words from all the dictionaries in the original list arr.

Below is the implementation of the above approach:

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

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

Time Complexity: O(n)
Auxiliary Space: O(n)

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


Output

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

The time complexity of this program is O(n^2), where n is the total number of dictionaries in the arr list
The auxiliary space complexity of this program is O(n), where n is the total number of unique keys in the dictionaries in the arr list.



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