Open In App

# Python | Extract specific keys from dictionary

We have a lot of variations and applications of dictionary containers in Python and sometimes, we wish to perform a filter of keys in a dictionary, i.e extracting just the keys which are present in the particular container. Let’s discuss certain ways in which this can be performed.

### Extract specific keys from dictionary using dictionary comprehension + items()

This problem can be performed by reconstruction using the keys extracted through the items function that wishes to be filtered and the dictionary function makes the desired dictionary.

## Python3

 `# initializing dictionary``test_dict ``=` `{``'nikhil'``: ``1``, ``"akash"``: ``2``, ``'akshat'``: ``3``, ``'manjeet'``: ``4``}` `# printing original list``print``(``"The original dictionary : "` `+` `str``(test_dict))` `# Using dictionary comprehension + items()``# Extracting specific keys from dictionary``res ``=` `{key: test_dict[key] ``for` `key ``in` `test_dict.keys()``       ``& {``'akshat'``, ``'nikhil'``}}` `# print result``print``(``"The filtered dictionary is : "` `+` `str``(res))`

Output :

`The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}The filtered dictionary is : {'akshat': 3, 'nikhil': 1}`

The time complexity of the given code is O(n), where n is the number of elements in the dictionary.

The space complexity of the given code is O(k), where k is the number of keys extracted from the dictionary.

### Extract specific keys from the dictionary using dict()

The dict() function can be used to perform this task by converting the logic performed using list comprehension into a dictionary.

## Python3

 `# initializing dictionary``test_dict ``=` `{``'nikhil'``: ``1``, ``"akash"``: ``2``, ``'akshat'``: ``3``, ``'manjeet'``: ``4``}` `# printing original list``print``(``"The original dictionary : "` `+` `str``(test_dict))` `# Using dict()``# Extracting specific keys from dictionary``res ``=` `dict``((k, test_dict[k]) ``for` `k ``in` `[``'nikhil'``, ``'akshat'``]``           ``if` `k ``in` `test_dict)` `# print result``print``(``"The filtered dictionary is : "` `+` `str``(res))`

Output :

`The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}The filtered dictionary is : {'akshat': 3, 'nikhil': 1}`

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), to store the keys and values in dictionary.

### Extract specific keys from dictionary using filter()

This method involves using the filter() function to filter the keys in the dictionary:

## Python3

 `#initializing dictionary``test_dict ``=` `{``'nikhil'``: ``1``, ``"akash"``: ``2``, ``'akshat'``: ``3``, ``'manjeet'``: ``4``}` `#printing original list``print``(``"The original dictionary : "` `+` `str``(test_dict))` `#Using filter()``#Extracting specific keys from dictionary``keys_to_extract ``=` `[``'nikhil'``, ``'akshat'``]``res ``=` `dict``(``filter``(``lambda` `item: item[``0``] ``in` `keys_to_extract, test_dict.items()))` `#print result``print``(``"The filtered dictionary is : "` `+` `str``(res))``#This code is contributed by Edula Vinay Kumar Reddy`

Output

```The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
```

Time complexity: O(n)
Auxiliary Space: O(n) for storing the result

### Extract specific keys from dictionary Using a for loop and a conditional statement

Initialize the dictionary:

1. Define the list of keys to be extracted.
2. Initialize an empty dictionary to store the extracted keys and their values.
3. Use a for loop and a conditional statement to check if each key in the dictionary is in the list of keys to be extracted. If it is, add the key-value pair to the extracted dictionary.
4. Print the extracted dictionary.

## Python3

 `# initializing dictionary``test_dict ``=` `{``'nikhil'``: ``1``, ``"akash"``: ``2``, ``'akshat'``: ``3``, ``'manjeet'``: ``4``}` `# printing original list``print``(``"The original dictionary is:"``, test_dict)` `# defining keys to extract``keys_to_extract ``=` `[``'nikhil'``, ``'akshat'``]` `# initializing an empty dictionary for extracted keys``extracted_dict ``=` `{}` `# extracting keys using a for loop and conditional statement``for` `key, value ``in` `test_dict.items():``    ``if` `key ``in` `keys_to_extract:``        ``extracted_dict[key] ``=` `value` `# printing the extracted dictionary``print``(``"The extracted dictionary is:"``, extracted_dict)`

Output

```The original dictionary is: {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The extracted dictionary is: {'nikhil': 1, 'akshat': 3}
```

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(k), where k is the number of keys to be extracted