Skip to main content
Tweeted twitter.com/StackCodeReview/status/1030016718473703424
deleted 6 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Better way to get Getting a key's value in a nested dictionary

I just found a now deleted question on Stack Overflow and solved the problem. The OP was asking for a way to get all the values contained by all 'PLU' keys. I solved it this way but because I am a newbie in python I was wondering if it would be an optimal/better way of doing this, maybe using iterators, I don't know.

Result should be:

Result should be:
234
32
33
334
61

def getPLU(data):
    for item in data:
        if type(data[item]) == dict:
            getPLU(data[item])
        else :
            if item == 'PLU':
                print (data[item])
    
    pass

def Main():
    times = 0
    Menu = {
      'PLU' : '234',
      'Salad': {
        'salad': {
            'ceaser':{
                'PLU': '32'
            },
            'italian':{
                'PLU': '33'
            }
        }
      },
      'Dessert': {
        'cookie': {
          'PLU': '334',
          'NAME': 'cookie ',
        }
      },
      'Appetizer': {
        'extra sauce': {
          'PLU': '61',
          'NAME': 'extra sauce',
        }
      }
    }
    getPLU(data=Menu)
    #print (getPLU(Menu))



if __name__ == '__main__':
    Main()

Better way to get a key's value in a nested dictionary

I just found a now deleted question on Stack Overflow and solved the problem. The OP was asking for a way to get all the values contained by all 'PLU' keys. I solved it this way but because I am a newbie in python I was wondering if it would be an optimal/better way of doing this, maybe using iterators, I don't know.

Result should be:
234
32
33
334
61

def getPLU(data):
    for item in data:
        if type(data[item]) == dict:
            getPLU(data[item])
        else :
            if item == 'PLU':
                print (data[item])
    
    pass

def Main():
    times = 0
    Menu = {
      'PLU' : '234',
      'Salad': {
        'salad': {
            'ceaser':{
                'PLU': '32'
            },
            'italian':{
                'PLU': '33'
            }
        }
      },
      'Dessert': {
        'cookie': {
          'PLU': '334',
          'NAME': 'cookie ',
        }
      },
      'Appetizer': {
        'extra sauce': {
          'PLU': '61',
          'NAME': 'extra sauce',
        }
      }
    }
    getPLU(data=Menu)
    #print (getPLU(Menu))



if __name__ == '__main__':
    Main()

Getting a key's value in a nested dictionary

I just found a now deleted question on Stack Overflow and solved the problem. The OP was asking for a way to get all the values contained by all 'PLU' keys. I solved it this way but because I am a newbie in python I was wondering if it would be an optimal/better way of doing this, maybe using iterators, I don't know.

Result should be:

234
32
33
334
61

def getPLU(data):
    for item in data:
        if type(data[item]) == dict:
            getPLU(data[item])
        else :
            if item == 'PLU':
                print (data[item])
    
    pass

def Main():
    times = 0
    Menu = {
      'PLU' : '234',
      'Salad': {
        'salad': {
            'ceaser':{
                'PLU': '32'
            },
            'italian':{
                'PLU': '33'
            }
        }
      },
      'Dessert': {
        'cookie': {
          'PLU': '334',
          'NAME': 'cookie ',
        }
      },
      'Appetizer': {
        'extra sauce': {
          'PLU': '61',
          'NAME': 'extra sauce',
        }
      }
    }
    getPLU(data=Menu)
    #print (getPLU(Menu))



if __name__ == '__main__':
    Main()
update wording
Source Link

I just found and solve a now deleted question on Stack Overflow and solved the problem. The OP was asking for a way to get all the values contained by all 'PLU' keys. I solved it this way but howbecause I am a newbie in python I was wondering if it would be an optimal/better way of doing this, maybe using iterators, I dunnodon't know.

Result should be:
234
32
33
334
61

def getPLU(data):
    for item in data:
        if type(data[item]) == dict:
            getPLU(data[item])
        else :
            if item == 'PLU':
                print (data[item])
    
    pass

def Main():
    times = 0
    Menu = {
      'PLU' : '234',
      'Salad': {
        'salad': {
            'ceaser':{
                'PLU': '32'
            },
            'italian':{
                'PLU': '33'
            }
        }
      },
      'Dessert': {
        'cookie': {
          'PLU': '334',
          'NAME': 'cookie ',
        }
      },
      'Appetizer': {
        'extra sauce': {
          'PLU': '61',
          'NAME': 'extra sauce',
        }
      }
    }
    getPLU(data=Menu)
    #print (getPLU(Menu))



if __name__ == '__main__':
    Main()

I just found and solve a now deleted question on Stack Overflow. The OP was asking for a way to get all the values contained by all 'PLU' keys. I solved it this way but how I am a newbie in python I was wondering if it would be an optimal/better way of doing this, maybe using iterators, I dunno.

Result should be:
234
32
33
334
61

def getPLU(data):
    for item in data:
        if type(data[item]) == dict:
            getPLU(data[item])
        else :
            if item == 'PLU':
                print (data[item])
    
    pass

def Main():
    times = 0
    Menu = {
      'PLU' : '234',
      'Salad': {
        'salad': {
            'ceaser':{
                'PLU': '32'
            },
            'italian':{
                'PLU': '33'
            }
        }
      },
      'Dessert': {
        'cookie': {
          'PLU': '334',
          'NAME': 'cookie ',
        }
      },
      'Appetizer': {
        'extra sauce': {
          'PLU': '61',
          'NAME': 'extra sauce',
        }
      }
    }
    getPLU(data=Menu)
    #print (getPLU(Menu))



if __name__ == '__main__':
    Main()

I just found a now deleted question on Stack Overflow and solved the problem. The OP was asking for a way to get all the values contained by all 'PLU' keys. I solved it this way but because I am a newbie in python I was wondering if it would be an optimal/better way of doing this, maybe using iterators, I don't know.

Result should be:
234
32
33
334
61

def getPLU(data):
    for item in data:
        if type(data[item]) == dict:
            getPLU(data[item])
        else :
            if item == 'PLU':
                print (data[item])
    
    pass

def Main():
    times = 0
    Menu = {
      'PLU' : '234',
      'Salad': {
        'salad': {
            'ceaser':{
                'PLU': '32'
            },
            'italian':{
                'PLU': '33'
            }
        }
      },
      'Dessert': {
        'cookie': {
          'PLU': '334',
          'NAME': 'cookie ',
        }
      },
      'Appetizer': {
        'extra sauce': {
          'PLU': '61',
          'NAME': 'extra sauce',
        }
      }
    }
    getPLU(data=Menu)
    #print (getPLU(Menu))



if __name__ == '__main__':
    Main()

I just found and solve a now deleted question at stackoverflow, theon Stack Overflow. The OP was asking for a way of how to get all the values contained by all 'PLU' keys. I solved it this way but how I am a newbie in python I was wonderwondering if it would be aan optimal/better way of doing this, maybe using iterators, iI dunno.

Result should be:
234
32
33
334
61

def getPLU(data):
    for item in data:
        if type(data[item]) == dict:
            getPLU(data[item])
        else :
            if item == 'PLU':
                print (data[item])
    
    pass

def Main():
    times = 0
    Menu = {
      'PLU' : '234',
      'Salad': {
        'salad': {
            'ceaser':{
                'PLU': '32'
            },
            'italian':{
                'PLU': '33'
            }
        }
      },
      'Dessert': {
        'cookie': {
          'PLU': '334',
          'NAME': 'cookie ',
        }
      },
      'Appetizer': {
        'extra sauce': {
          'PLU': '61',
          'NAME': 'extra sauce',
        }
      }
    }
    getPLU(data=Menu)
    #print (getPLU(Menu))



if __name__ == '__main__':
    Main()

I just found and solve a now deleted question at stackoverflow, the OP was asking for a way of how to get all the values contained by all 'PLU' keys. I solved it this way but how I am a newbie in python I was wonder if would be a optimal/better way of doing this, maybe using iterators, i dunno.

Result should be:
234
32
33
334
61

def getPLU(data):
    for item in data:
        if type(data[item]) == dict:
            getPLU(data[item])
        else :
            if item == 'PLU':
                print (data[item])
    
    pass

def Main():
    times = 0
    Menu = {
      'PLU' : '234',
      'Salad': {
        'salad': {
            'ceaser':{
                'PLU': '32'
            },
            'italian':{
                'PLU': '33'
            }
        }
      },
      'Dessert': {
        'cookie': {
          'PLU': '334',
          'NAME': 'cookie ',
        }
      },
      'Appetizer': {
        'extra sauce': {
          'PLU': '61',
          'NAME': 'extra sauce',
        }
      }
    }
    getPLU(data=Menu)
    #print (getPLU(Menu))



if __name__ == '__main__':
    Main()

I just found and solve a now deleted question on Stack Overflow. The OP was asking for a way to get all the values contained by all 'PLU' keys. I solved it this way but how I am a newbie in python I was wondering if it would be an optimal/better way of doing this, maybe using iterators, I dunno.

Result should be:
234
32
33
334
61

def getPLU(data):
    for item in data:
        if type(data[item]) == dict:
            getPLU(data[item])
        else :
            if item == 'PLU':
                print (data[item])
    
    pass

def Main():
    times = 0
    Menu = {
      'PLU' : '234',
      'Salad': {
        'salad': {
            'ceaser':{
                'PLU': '32'
            },
            'italian':{
                'PLU': '33'
            }
        }
      },
      'Dessert': {
        'cookie': {
          'PLU': '334',
          'NAME': 'cookie ',
        }
      },
      'Appetizer': {
        'extra sauce': {
          'PLU': '61',
          'NAME': 'extra sauce',
        }
      }
    }
    getPLU(data=Menu)
    #print (getPLU(Menu))



if __name__ == '__main__':
    Main()
Source Link
Nestor
  • 301
  • 1
  • 3
  • 10
Loading