Write a program that prints the number of unique keys in a list of dictionaries.


Topic: Write a program that prints the number of unique keys in a list of dictionaries.

Solution

list_of_dicts = [{"key1": "val1", "Country": "India"}, 
                 {"Country": "USA", "foo": "bar"},
                 {"foo": "bar", "foo2":"bar2"}]
unique_keys = []
for d in list_of_dicts:
  for key in d:
    if key not in unique_keys:
      unique_keys.append(key)
print(f"Number of unique keys: {len(unique_keys)}")



List all Python Programs