Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from six import iteritems
- def get_all_keys(dictionary, result_list=[]):
- """
- Method to get all keys from a nested dictionary as a List
- Args:
- dictionary: Nested dictionary
- Returns:
- List of keys in the dictionary
- """
- for key, value in iteritems(dictionary):
- if isinstance(value, dict):
- result_list.append(key)
- get_all_keys(dictionary=value, result_list=result_list)
- else:
- result_list.append(key)
- return result_list
- details = {
- "hardware_details": {
- "model_name": "MacBook Pro",
- "processor_details": {
- "processor_name": "Intel Core i7",
- "processor_speed": "2.7 GHz",
- "core_details": {
- "total_numberof_cores": "4",
- "l2_cache(per_core)": "256 KB"
- }
- },
- "total_number_of_cores": "4",
- "memory": "16 GB",
- },
- "os_details": {
- "product_version": "10.13.6",
- "build_version": "17G65"
- },
- "name": "Test",
- "date": "YYYY-MM-DD HH:MM:SS"
- }
- print get_all_keys(dictionary=details)
Add Comment
Please, Sign In to add comment