 Open in App
Not now

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

• Last Updated : 22 Dec, 2022

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”}.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

## 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']`

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']`

My Personal Notes arrow_drop_up
Related Articles