Home > Software engineering >  Python 3.8 - Unable to update dictionary
Python 3.8 - Unable to update dictionary

Time:02-10

Hello I have this below dictionary that I want to update

{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect":"Allow",
         "Principal":{
            "Federated":"arn:aws:iam::111111111111:saml-provider/Test"
         },
         "Action":"sts:AssumeRoleWithSAML",
         "Condition":{
            "StringEquals":{
               "SAML:aud":"https://signin.aws.amazon.com/saml"
            }
         }
      }
   ]
}

Want to update it to

{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect":"Allow",
         "Principal":{
            "Federated":[
            "arn:aws:iam::111111111111:saml-provider/Test", 
            "arn:aws:iam::111111111111:saml-provider/Test2"
            ]
         },
         "Action":"sts:AssumeRoleWithSAML",
         "Condition":{
            "StringEquals":{
               "SAML:aud":"https://signin.aws.amazon.com/saml"
            }
         }
      }
   ]
}

i.e. add "arn:aws:iam::111111111111:saml-provider/Test2" to "Federated" and also make it a list. Below is my code

    new_arn = "arn:aws:iam::111111111111:saml-provider/Test2"
    my_dict = {
   "Version":"2012-10-17",
   "Statement":[
      {
             "Effect":"Allow",
             "Principal":{
                "Federated":[
                "arn:aws:iam::111111111111:saml-provider/Test",
                ]
             },
             "Action":"sts:AssumeRoleWithSAML",
             "Condition":{
                "StringEquals":{
                   "SAML:aud":"https://signin.aws.amazon.com/saml"
                }
             }
          }
       ]
    }
    for b in my_dict['Statement']:
        updated_arn = f"['{b['Principal']['Federated']}', {new_arn}]"
        b['Principal']['Federated']: updated_arn

    print(my_dict)

I am bit new to python and I am not sure what am I doing wrong the dict is not getting updated. Can someone please provide some guidance on what I may be doing wrong

CodePudding user response:

As folks commented, you're constructing a string that looks like a list here:

    for b in my_dict['Statement']:
        updated_arn = f"['{b['Principal']['Federated']}', {new_arn}]"
        b['Principal']['Federated']: updated_arn

You can create a real list instead:

    for b in my_dict['Statement']:
        updated_arn = [b['Principal']['Federated'], new_arn]
        b['Principal']['Federated'] = updated_arn
        # note it's `=` not `:` here

Edit: If Federated is sometimes a string, and sometimes already a list, you'll need to check its type and act accordingly:

    for b in my_dict['Statement']:
        federated = b['Principal']['Federated']
        if isinstance(federated, list):
            federated.append(new_arn)
            # (which updates the list within the dict, no need to assign back to the dict)
        else:
            b['Principal']['Federated'] = [federated, new_arn]

CodePudding user response:

You can append to the current value of "Federated" as follows:

for b in my_dict['Statement']:
       b['Principal']['Federated'].append(new_arn)

CodePudding user response:

So, i am a bit new to Python to, but i think that if u want to update that library, u can write in the terminal, write something like that:

pip install --upgrade library_name

enter image description here , like that, so good luck learning Python!!

  •  Tags:  
  • Related