Click here to Skip to main content
15,937,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a dictionary like this where some of the values are "None" and others are keys in the same dictionary

group = {
    "Duties & Taxes": "Current Liabilities",
    "Excise": "Duties & Taxes",
    "Bank Accounts": "Current Assets",
    "Branch / Divisions": "None",
    "Capital Account": "None",
    "Cash-in-hand": "Current Assets",
    "Current Assets": "None",
    "Current Capital Account": "Capital Account",
    "Current Liabilities": "None",
    "CWIP": "None",
    "Deposits (Asset)": "Current Assets",
    "Direct Expenses": "None",
    "Direct Incomes": "None",
    "GST": "Duties & Taxes"
}


I want to replace all the values that are not "None" to something like this:
{
    "Duties & Taxes": "Current Liabilities",
    "Excise": "Current Liabilities",
    "Bank Accounts": "Current Assets",
    "Branch / Divisions": "None",
    "Capital Account": "None",
    "Cash-in-hand": "Current Assets",
    "Current Assets": "None",
    "Current Capital Account": "Capital Account",
    "Current Liabilities": "None",
    "CWIP": "None",
    "Deposits (Asset)": "Current Assets",
    "Direct Expenses": "None",
    "Direct Incomes": "None",
    "GST": "Current Liabilities"
}


What I have tried:

I was not able to do this recursively.
This is the code that works if the depth is less than 6.

def get_up(group):
    # print(group)
    if group_dict[group] == "None":
        return group
    if group_dict[group_dict[group]] == "None":
        return group_dict[group]
    if group_dict[group_dict[group_dict[group]]] == "None":
        return group_dict[group_dict[group]]
    if group_dict[group_dict[group_dict[group_dict[group]]]] == "None":
        return group_dict[group_dict[group_dict[group]]]
    if group_dict[group_dict[group_dict[group_dict[group_dict[group]]]]] == "None":
        return group_dict[group_dict[group_dict[group_dict[group]]]]
    if group_dict[group_dict[group_dict[group_dict[group_dict[group_dict[group]]]]]] == "None":
        return group_dict[group_dict[group_dict[group_dict[group_dict[group]]]]]
    else:
        return group_dict[group_dict[group_dict[group_dict[group_dict[group]]]]]
Posted
Updated 23-Feb-22 22:35pm

Try this:
Python
for key, value in group.items():
    if value in group and group[value] != "None":
        group[key] = group[value]
print(group)
 
Share this answer
 
Comments
PoornaRecharla 24-Feb-22 6:28am    
It is returning "None" for almost all the items.
Richard MacCutchan 24-Feb-22 6:48am    
It works for me.
PoornaRecharla 24-Feb-22 7:21am    
It works. Thank you.
Don't understand why you want to do it recursively. Check below

Python
group = {
    "Duties & Taxes": "Current Liabilities",
    "Excise": "Duties & Taxes",
    "Bank Accounts": "Current Assets",
    "Branch / Divisions": "None",
    "Capital Account": "None",
    "Cash-in-hand": "Current Assets",
    "Current Assets": "None",
    "Current Capital Account": "Capital Account",
    "Current Liabilities": "None",
    "CWIP": "None",
    "Deposits (Asset)": "Current Assets",
    "Direct Expenses": "None",
    "Direct Incomes": "None",
    "GST": "Duties & Taxes"
}
group.update({ "Excise": "Current Liabilities", "GST": "Current Liabilities"})
print(group)
 
Share this answer
 
Comments
PoornaRecharla 24-Feb-22 6:13am    
This is just a sample list. I have around 500 items and the data changes frequently.
So, I need to update it regularly in my program.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900