Get all Unique Keys from a List of Dictionaries - Python
Last Updated :
05 Feb, 2025
Our task is to get all unique keys from a list of dictionaries and we are given a list where each element is a dictionary, we need to extract and return a list of keys that appear across all dictionaries. The result should contain each key only once regardless of how many times it appears. For example, if we have the following list of dictionaries: a = [{'my': 1, 'name': 2}, {'is': 1, 'my': 3}, {'ria': 2}] then the unique keys in the list would be: ['ria', 'my', 'is', 'name'].
Using a loop and update()
We can iterate through each dictionary in the list and add the keys to a set and since a set automatically handles uniqueness we don’t need to worry about duplicate keys.
Python
a = [{'my': 1, 'name': 2}, {'is': 1, 'my': 3}, {'ria': 2}]
s = set()
for d in a:
s.update(d.keys())
print(list(s))
Output['name', 'is', 'ria', 'my']
Explanation:
- We initialize an empty set s then for each dictionary d in the list "a" we update the set with the dictionary's keys using s.update(d.keys()).
- Since sets only store unique values hence any duplicate keys are automatically discarded.
Using itertools.chain()
We can use itertools.chain() to chain together the keys of all dictionaries in the list and convert them into a set to ensure uniqueness.
Python
from itertools import chain
a = [{'my': 1, 'name': 2}, {'is': 1, 'my': 3}, {'ria': 2}]
s = set(chain.from_iterable(d.keys() for d in a))
print(list(s))
Output['my', 'is', 'name', 'ria']
Explanation:
- We use itertools.chain() to chain the keys from all dictionaries into a single iterable.
- from_iterable() allows us to flatten the iterable of key lists into a single sequence and we then convert the result into a set to ensure all keys are unique.
Using reduce()
We can also use reduce() from the functools module to combine all the dictionaries and extract the unique keys.
Python
from functools import reduce
a = [{'my': 1, 'name': 2}, {'is': 1, 'my': 3}, {'ria': 2}]
s = reduce(lambda acc, d: acc.union(d.keys()), a, set())
print(list(s))
Output['is', 'name', 'ria', 'my']
Explanation:
- reduce() applies a function (in this case, acc.union(d.keys())) cumulatively to the items in "a".
- It starts with an empty set (set()) as the initial value (acc) and adds the keys from each dictionary.
- union() ensures that only unique keys are kept in the set.
Using collections.Counter
We can use collections.Counter to count the occurrence of each key across all dictionaries, the keys with any count will be considered unique as all keys are stored in the dictionary.
Python
from collections import Counter
a = [{'my': 1, 'name': 2}, {'is': 1, 'my': 3}, {'ria': 2}]
s = set(Counter(k for d in a for k in d.keys()))
print(list(s))
Output['is', 'name', 'my', 'ria']
Explanation:
- Counter is used here to count the occurrences of keys across all dictionaries.
- The keys are generated by iterating through each dictionary and using k for d in arr for k in d.keys().
Similar Reads
How to use a List as a key of a Dictionary in Python 3? In Python, we use dictionaries to check if an item is present or not . Dictionaries use key:value pair to search if a key is present or not and if the key is present what is its value . We can use integer, string, tuples as dictionary keys but cannot use list as a key of it . The reason is explained
3 min read
Get Key from Value in Dictionary - Python The goal is to find the keys that correspond to a particular value. Since dictionaries quickly retrieve values based on keys, there isn't a direct way to look up a key from a value. Using next() with a Generator ExpressionThis is the most efficient when we only need the first matching key. This meth
5 min read
Flatten given list of dictionaries - Python We are given a list of dictionaries, and the task is to combine all the key-value pairs into a single dictionary. For example, if we have: d = [{'a': 1}, {'b': 2}, {'c': 3}] then the output will be {'a': 1, 'b': 2, 'c': 3}Using the update() methodIn this method we process each dictionary in the list
3 min read
How to Compare List of Dictionaries in Python Comparing a list of dictionaries in Python involves checking if the dictionaries in the list are equal, either entirely or based on specific key-value pairs. This process helps to identify if two lists of dictionaries are identical or have any differences. The simplest approach to compare two lists
3 min read
Python - Unique value keys in a dictionary with lists as values Sometimes, while working with Python dictionaries, we can have problem in which we need to extract keys with values that are unique (there should be at least one item not present in other lists), i.e doesn't occur in any other key's value lists. This can have applications in data preprocessing. Lets
4 min read
Get Unique Values from a List in Python Getting unique values from a list in Python allows you to remove duplicates and work with distinct elements. For example, given the list a = [1, 2, 1, 1, 3, 4, 3, 3, 5], you might want to extract the unique values [1, 2, 3, 4, 5]. There are various efficient methods to extract unique values from a l
3 min read