Find Keys Common to All Dicts

Medium ⏱ 12 min 47% acceptance ★★★★☆ 4.4
Write a function common_keys(dict_list) that takes a list of dicts and returns a sorted list of keys that are present in every dict in dict_list. If dict_list is empty, return an empty list.

Examples

Example 1
Input
common_keys([{'a': 1, 'b': 2}, {'a': 5, 'b': 6, 'c': 7}, {'a': 9, 'b': 0}])
Output
['a', 'b']
Explanation

'a' and 'b' appear in all three dicts; 'c' only in the second.

Constraints

  • Return sorted order.
  • Empty input list returns [].

Topics

Dictionariesset operations

Companies

OracleIBM

Hints

Hint 1

Convert each dict's keys to a set, then intersect all sets with `set.intersection(*sets)`.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.