Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python extract specific keys from dictionary

# Basic syntax:
{key: your_dict[key] for key in your_dict.keys() and {'key_1', 'key_2'}}
# Where this uses list comprehension for dictionaries to make a new dictionary
#	with the keys that are found in the set

# Example usage:
your_dict = {'key_1': 1, 'key_2': 2, 'key_3': 3}
{key: your_dict[key] for key in your_dict.keys() and {'key_1', 'key_2'}}
--> {'key_1': 1, 'key_2': 2}
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #python #extract #specific #keys #dictionary
ADD COMMENT
Topic
Name
2+3 =