how do i Itterate trough a dictionary for a specific key then ading it to a new dictionary

0 votes

i know this is a noob question but im an new learner to Python and im struggling with dictionary's.

i think my code is right but im not sure.

what i am trying to achieve is that i want to iterate trough a dictionary with a list items with a dictionary in it. If the value is == None then that key Should be skipped. but if there is a value. i want to append the Key:Value pair to the pmp_dict = {}

here is my code:

    input_dict = {
    "operation": {
        "Details": {
            "ACCOUNTLIST": {
                "RESOURCENAME": "abbas",
                "ACCOUNTNAME": "account_name",
                "RESOURCETYPE":None,
                "PASSWORD": "password"
            }
        }
    }
}


pmp_dict= {
    "operation": {
        "Details": {
            "ACCOUNTLIST": [
                {
                
                }
            ]
        }
    }
}

print(pmp_dict)

for list_item in input_dict["operation"]["Details"]["ACCOUNTLIST"]:
    for key, value in list_item.items():
        if value == None:
            pass
        else:
            pmp_dict["operation"]["Details"]["ACCOUNTLIST"][key]=value

what Im struggling with is the last 5 lines. how do i write so that i achieve the desired outcome? i know what i want to do but not the syntax for it.

when i do this i get error:

Traceback (most recent call last):
  File "<string>", line 29, in <module>
AttributeError: 'str' object has no attribute 'items'

Apr 7, 2021 in Python by Said

edited Mar 4, 2025 300 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP