Python Program to extract Dictionaries with given Key from a list of dictionaries

Python's implementation of a data structure known more commonly as an associative array is a dictionary. A dictionary is made up of a group of key-value pairs. Each key-value combination corresponds to a key and its corresponding value.

In this article, we will learn how to extract dictionaries that contain a specific key from a list of dictionaries using three different approaches.

Problem Example

Let's understand the problem with an example ?

# Input list of dictionaries
input_list = [
    {'hello': 3, 'tutorialspoint': 6}, 
    {'python': 9},
    {'users': 12, 'users': 15, 'hello': 18},
    {'users': 21}
]

# We want to extract dictionaries containing the key 'users'
target_key = 'users'
print("Input list:", input_list)
print("Target key:", target_key)
Input list: [{'hello': 3, 'tutorialspoint': 6}, {'python': 9}, {'users': 15, 'hello': 18}, {'users': 21}]
Target key: users

From this list, only the last two dictionaries contain the key 'users', so our result should be [{'users': 15, 'hello': 18}, {'users': 21}].

Method 1: Using List Comprehension and keys()

List comprehension provides a concise way to create lists based on existing iterables. The keys() method returns a view of all dictionary keys ?

# Input list of dictionaries
input_list = [
    {'hello': 3, 'tutorialspoint': 6}, 
    {'python': 9},
    {'users': 12, 'users': 15, 'hello': 18},
    {'users': 21}
]

# Target key to search for
target_key = 'users'

# Extract dictionaries containing the target key
result = [dictionary for dictionary in input_list if target_key in dictionary.keys()]

print("Dictionaries containing 'users' key:")
print(result)
Dictionaries containing 'users' key:
[{'users': 15, 'hello': 18}, {'users': 21}]

Method 2: Using filter() and Lambda

The filter() function filters elements from a sequence based on a condition. Lambda functions provide anonymous functions for simple operations ?

# Input list of dictionaries
input_list = [
    {'hello': 3, 'tutorialspoint': 6}, 
    {'python': 9},
    {'users': 12, 'users': 15, 'hello': 18},
    {'users': 21}
]

target_key = 'users'

# Filter dictionaries using lambda function
result = list(filter(lambda dictionary: target_key in dictionary.keys(), input_list))

print("Dictionaries containing 'users' key:")
print(result)
Dictionaries containing 'users' key:
[{'users': 15, 'hello': 18}, {'users': 21}]

Method 3: Using operator.countOf()

The operator.countOf() method counts occurrences of a value in a sequence. We can use it to check if a key exists by counting its occurrences ?

import operator as op

# Input list of dictionaries
input_list = [
    {'hello': 3, 'tutorialspoint': 6}, 
    {'python': 9},
    {'users': 12, 'users': 15, 'hello': 18},
    {'users': 21}
]

target_key = 'users'

# Use countOf to check key existence
result = [dictionary for dictionary in input_list 
          if op.countOf(list(dictionary.keys()), target_key) > 0]

print("Dictionaries containing 'users' key:")
print(result)
Dictionaries containing 'users' key:
[{'users': 15, 'hello': 18}, {'users': 21}]

Comparison

Method Readability Performance Best For
List Comprehension High Fast Simple key checking
filter() + lambda Medium Fast Complex filtering logic
operator.countOf() Low Slower When you need occurrence counts

Conclusion

List comprehension with in operator is the most readable and efficient approach for extracting dictionaries by key. Use filter() when you need more complex filtering logic, and operator.countOf() when you specifically need to count key occurrences.

Updated on: 2026-03-27T13:03:01+05:30

247 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements