-1

I have a relatively simple nested dictionary as below:

emp_details = {
    'Employee': {
        'jim': {'ID':'001', 'Sales':'75000', 'Title': 'Lead'}, 
        'eva': {'ID':'002', 'Sales': '50000', 'Title': 'Associate'}, 
        'tony': {'ID':'003', 'Sales': '150000', 'Title': 'Manager'}
    }
}

I can get the sales info of 'eva' easily by:

print(emp_details['Employee']['eva']['Sales'])

but I'm having difficulty writing a statement to extract information on all employees whose sales are over 50000.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Tony R
  • 13
  • 1
  • 2
    What kind of difficulties? what have you tried? what did not work? – DeepSpace Jun 26 '22 at 07:53
  • 1
    `emp_details['Employee']` is a dict. You can loop over the keys and values of a dictionary: [Iterating over dictionaries using 'for' loops](https://stackoverflow.com/q/3294889/2745495) – Gino Mempin Jun 26 '22 at 08:02

2 Answers2

0

Your Sales is of String type.

Therefore, we can do something like this to get the information of employees whose sales are over 50000 : -

Method1 : If you just want to get the information : -

emp_details={'Employee':{'jim':{'ID':'001', 'Sales':'75000', 'Title': 'Lead'}, \
                         'eva':{'ID':'002', 'Sales': '50000', 'Title': 'Associate'}, \
                         'tony':{'ID':'003', 'Sales': '150000', 'Title': 'Manager'}
                         }}
                         
for emp in emp_details['Employee']:
    if int(emp_details['Employee'][emp]['Sales']) > 50000:
        print(emp_details['Employee'][emp])

It print outs to -:

{'ID': '001', 'Sales': '75000', 'Title': 'Lead'}
{'ID': '003', 'Sales': '150000', 'Title': 'Manager'}

Method2 : You can use Dict and List comprehension to get complete information : -

emp_details={'Employee':{'jim':{'ID':'001', 'Sales':'75000', 'Title': 'Lead'}, \
                         'eva':{'ID':'002', 'Sales': '50000', 'Title': 'Associate'}, \
                         'tony':{'ID':'003', 'Sales': '150000', 'Title': 'Manager'}
                         }}
                         
emp_details_dictComp = {k:v for k,v in list(emp_details['Employee'].items()) if int(v['Sales']) > 50000}
print(emp_details_dictComp)

emp_details_listComp = [(k,v) for k,v in list(emp_details['Employee'].items()) if int(v['Sales']) > 50000]
print(emp_details_listComp)

Result : -

{'jim': {'ID': '001', 'Sales': '75000', 'Title': 'Lead'}, 'tony': {'ID': '003', 'Sales': '150000', 'Title': 'Manager'}}

[('jim', {'ID': '001', 'Sales': '75000', 'Title': 'Lead'}), ('tony', {'ID': '003', 'Sales': '150000', 'Title': 'Manager'})]
0

You can't use one statement because the list initializer expression can't have an if without an else.

Use a for loop:

result = {} # dict expression
result_list = [] # list expression using (key, value)
for key, value in list(emp_details['Employee'].items())): # iterate from all items in dictionary
    if int(value['Sales']) > 50000: # your judgement
        result[key] = value # add to dict
        result_list.append((key, value)) # add to list
print(result)
print(result_list)
# should say:
'''
{'jim': {'ID':'001', 'Sales':'75000', 'Title': 'Lead'}, 'tony': {'ID':'003', 'Sales': '150000', 'Title': 'Manager'}}
[('jim', {'ID':'001', 'Sales':'75000', 'Title': 'Lead'}), ('tony', {'ID':'003', 'Sales': '150000', 'Title': 'Manager'})]
'''
jett8998
  • 888
  • 2
  • 15