Skip to content

girdharv18/Challenge-3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

We have a nested object. We would like a function where you pass in the object and a key and get back the value.

What is nested object?

The basic definition of an object in JavaScript is a container for named values called properties (keys). Sometimes, we need to create an object inside another object. In this case, it's called a nested object.

And In Python, a nested dictionary is a dictionary inside a dictionary. It's a collection of dictionaries into one single dictionary.

Below is the code for the same.

# Code

def getKey(obj: dict):
  keys = list(obj)
  if len(keys) != 1:
      raise Exception('either multiple keys or empty dict found')
  else:
      return keys[0]


def getNestedValue(obj: dict, key: str, isFound = False):
  # print(obj, key, isFound)
  if type(obj) is not dict and not isFound:
      return None
  if (isFound or (key in obj.keys())) :
      if type(obj[key]) is dict:
          return getNestedValue(obj[key], getKey(obj[key]), True)
      else:
          # print(f'obj[getKey(obj)]: {obj[getKey(obj)]}')
          return obj[getKey(obj)]
  else:
      nestedKey = getKey(obj)
      return getNestedValue(obj[nestedKey], key, False)

if __name__ == '__main__':
  obj = {'a': {'b': {'c': 'd'}}}
  value = getNestedValue(obj, 'c')
  print(value)

Output

d

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors