How to fetch the value in dictionary by checking the key mapped in the dict

Nons

Dictionary is below

my = {
  "1": {
    "exclude": 'A,B',
    "column": "value",
    "output": "Out1",
    "Out1": "Cost",
    "Out2": "Rev"
  },
  "2": {
    "exclude": 'None',
    "column": "value",
    "output": "Out2",
    "Out1": "Cost",
    "Out2": "Rev"
  }
}
  • I need to check column key value (if lower(my[column])) == lower(value)
  • I have exclude_list = ['A','B','C']
  • I need to the exclude key, if exclude is 'A' check the exclude_list = ['A','B','C'] take the value B,C. If the exclude key is 'A,B' take the value C as shown in the example.
  • I need to check the output key if output key is Out1 then fetch value of Out1 and mapped to EndOutput

Expected Out

{'1': {'exclude': ['C'], 'EndOutput': 'Cost'},
 '2': {'exclude': ['A', 'B', 'C'], 'EndOutput': 'Rev'}}

exclude_list = ['A','B','C']
for k,v in my.items():
    if v['column'].lower() == 'value'.lower:
     
   
A.B

You can iterate over keys 1,2 .. N with for e in my: and then calculate exclude with help of set() difference as

and extract things like this

 exc = set(['A','B','C']) - set(my[e]["exclude"].split(",") if len(my[e]["exclude"].split(",")) else [] )

next you can take value of out1 or out2 depending on the value of output as

my[e][my[e]["output"]]

Insert in new dictionary as

newdict[e] = {'exclude': list(exc) ,'EndOutput':my[e][my[e]["output"]]}

Overall, this is how you can iterate

newdict = {}
for e in my:
     if my[e]['column'].lower() == 'value':
             exc = set(['A','B','C']) - set(my[e]["exclude"].split(",") if len(my[e]["exclude"].split(",")) else [] )
             newdict[e] = {'exclude': list(exc) ,'EndOutput':my[e][my[e]["output"]]}

printing newdict will produce this output

'1': {'EndOutput': 'Cost', 'exclude': ['C'] },
'2': {'EndOutput': 'Rev', 'exclude': ['A', 'B', 'C'] }}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to add a key:pair value to a nested dictionary, mapped to an item

Checking for key and value in python dictionary

Checking the value of a key in a dictionary within a list?

Checking whether a key or value exists in a dictionary

How would I put a dict {key: value} in it's designated key in a dictionary so that it is {key: {key: value}} after counting value

fetch key based on value from python dictionary

How to get key and value of Firestore mapped object

How to fetch key from any of assigned values in a multi-value dictionary

Most pythonic way of checking if a Key is in a dictionary and Value is not None

Is checking if key is in dictionary and getting it's value in the same "if" safe?

Vectorized way of checking dataframe values (as key, value tuple) against a dictionary?

Adding multiple key value pairs to a python dictionary and then checking for duplicate values

Checking a if a string contains a string value from a dictionary and return the appropriate key

How to set a value of a non-mapped parameter of an entity during fetch

How to access key by value in a dictionary?

How to change the key and value of a dictionary

How to change the key value of a dictionary?

How to change the value of a key in a dictionary which is in a dictionary

Checking for key in dict during comprehension

Matching one dictionary key to another dictionary's value and returning a file with dict1.key and dict2.value

Ansible: how to fetch a value from a dictionary if the dictionary MIGHT be undefined

Python: Checking if a key is part of a dictionary

How to get the value of a value as a key of a dictionary

How to make a dictionary value the key of another value?

How to convert an array into an object in javascript with mapped key-value pairs?

How to append a key before a value in Python dict?

How to modify dict elements based on key value?

How iterate (key, value) boost::python:dict

how to find the key with the highest value in a dict of dicts?