Guest User

Untitled

a guest
Sep 20th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. from six import iteritems
  2.  
  3. def get_all_keys(dictionary, result_list=[]):
  4. """
  5. Method to get all keys from a nested dictionary as a List
  6.  
  7. Args:
  8. dictionary: Nested dictionary
  9.  
  10. Returns:
  11. List of keys in the dictionary
  12. """
  13. for key, value in iteritems(dictionary):
  14. if isinstance(value, dict):
  15. result_list.append(key)
  16. get_all_keys(dictionary=value, result_list=result_list)
  17. else:
  18. result_list.append(key)
  19. return result_list
  20.  
  21.  
  22. details = {
  23. "hardware_details": {
  24. "model_name": "MacBook Pro",
  25. "processor_details": {
  26. "processor_name": "Intel Core i7",
  27. "processor_speed": "2.7 GHz",
  28. "core_details": {
  29. "total_numberof_cores": "4",
  30. "l2_cache(per_core)": "256 KB"
  31. }
  32. },
  33. "total_number_of_cores": "4",
  34. "memory": "16 GB",
  35. },
  36. "os_details": {
  37. "product_version": "10.13.6",
  38. "build_version": "17G65"
  39. },
  40. "name": "Test",
  41. "date": "YYYY-MM-DD HH:MM:SS"
  42. }
  43.  
  44. print get_all_keys(dictionary=details)
Add Comment
Please, Sign In to add comment