mary jerson
mary jerson

Reputation: 71

How to get the value of a nested dictionary inside of a list?

I'm having issues with a small python function, currently I have this structure:

dict_conf = {'conf_storage':
            {'option1':[
                {'number':'20169800'},
                {'name':'usb'},
                {'description':'16gb'},
                {'qty':'1'},
                {'vendor=':'XLR'},
                {'online':'Yes'}],
            'option2':[
                {'number':'20161789'},
                {'name':'hardrive'},
                {'description':'128gb'},
                {'qty':'1'},
                {'vendor=':'KBW'},
                {'online':'NO'}]},
        'conf_grph':
                {'option1':[
                    {'number':'20170012'},
                    {'name':'HD_screen'},
                    {'description':'1080p'},
                    {'qty':'1'},
                    {'vendor=':'PWD'},
                    {'online':'Yes'}]}}

conf_type = raw_input("Enter the conf type: ")
option = raw_input("Enter the option")

I want to find "number" value, for example if the user enters:

conf_type = "conf_storage"
number = "20169800"

Then print the value and a message saying: "You entered a valid number, it is: 20169800"

My idea to solve this is to iterate and return every value equal to what the user entered.

if I use iteritems I get every element and then I can put that into a for loop but after that I'm not sure how can I get inside the list that contains the dictionaries and retrieve the value of "number" key.

Please if you have an answer can you explain it to me, I mean how you found what to do.

Thanks

Upvotes: 1

Views: 907

Answers (4)

Michael Brenndoerfer
Michael Brenndoerfer

Reputation: 4096

A simple solution might be to just iterate over all elements.

conf_type = "conf_storage"
number = "20169800"

if dict_conf[conf_type]:
    for key, value in dict_conf[conf_type].items():
        for v in value:
            for k,num in v.items():
                if num == number:
                    print('found')

Upvotes: 2

Rajanya Dhar
Rajanya Dhar

Reputation: 166

This is the complete working code:

dict_conf = {'conf_storage':
            {'option1':[
                {'number':'20169800'},
                {'name':'usb'},
                {'description':'16gb'},
                {'qty':'1'},
                {'vendor=':'XLR'},
                {'online':'Yes'}],
            'option2':[
                {'number':'20161789'},
                {'name':'hardrive'},
                {'description':'128gb'},
                {'qty':'1'},
                {'vendor=':'KBW'},
                {'online':'NO'}]},
    'conf_grph':
            {'option1':[
                {'number':'20170012'},
                {'name':'HD_screen'},
                {'description':'1080p'},
                {'qty':'1'},
                {'vendor=':'PWD'},
                {'online':'Yes'}]}}


conf_type = raw_input("Enter the conf type: ")
option = raw_input("Enter the option: ")
number = raw_input("Enter the number for validation: ")

dict_options = dict_conf[conf_type]
option_list = dict_options[option]

for elem_dict in option_list:
    if 'number' in elem_dict.keys():
        if elem_dict['number'] == number:
            print "You entered a valid number, it is: " + number

Upvotes: 0

AChampion
AChampion

Reputation: 30288

Transform the numbers into a list and check if the entered number is in the list e.g.:

conf_type = "conf_storage"
number = "20169800"

if number in [option[0]['number'] for option in dict_conf[conf_type].values()]:
    print("You entered a valid number, it is: {}".format(number))
# You entered a valid number, it is: 20169800

Upvotes: 0

kjmerf
kjmerf

Reputation: 4345

This should do it:

print "You entered a valid number, it is:", dict_conf[conf_type][option][0]['number']

https://repl.it/I3tr

Upvotes: 0

Related Questions