Home > Back-end >  Python dictionary not getting updated
Python dictionary not getting updated

Time:02-14

Hello I have below code where I am trying to add elements to a dict

for ent in NACLS:
    set_filter = {'Name': 'network-acl-id', 'Values': [naclID]}
    print(set_filter)
    set_filter.update({'Name': 'entry.rule-number', 'Values': [str(ent[0])]})
    print(set_filter)

The output for the above code is

{'Name': 'network-acl-id', 'Values': ['acl-08128a2540eb']}
{'Name': 'entry.rule-number', 'Values': ['50']}
{'Name': 'network-acl-id', 'Values': ['acl-08128a2540eb']}
{'Name': 'entry.rule-number', 'Values': ['60']}
{'Name': 'network-acl-id', 'Values': ['acl-08128a2540eb']}
{'Name': 'entry.rule-number', 'Values': ['70']}

Whereas I was wanting it to update the existing dictionary but somehow it's getting overwritten. Expected output

{'Name': 'network-acl-id', 'Values': ['acl-08128a2540eb']}, {'Name': 'entry.rule-number', 'Values': ['50']}
{'Name': 'network-acl-id', 'Values': ['acl-08128a2540eb']}, {'Name': 'entry.rule-number', 'Values': ['60']}
{'Name': 'network-acl-id', 'Values': ['acl-08128a2540eb']}, {'Name': 'entry.rule-number', 'Values': ['70']}

can someone please guide me on how I can achieve this?

CodePudding user response:

The update method adds {key:value} pairs to a dictionary unless the key already exists, in which case it just updates the value to the new value.

From the look of your expected output it seems you want a list of dictionaries all with the same keys but different values.

try this:

set_filter = []
for ent in NACLS:
    set_filter.append({'Name': 'network-acl-id', 'Values': [naclID]})
    set_filter.append({'Name': 'entry.rule-number', 'Values': [str(ent[0])]})
    print(set_filter)

CodePudding user response:

I didn't understand what is the problem here. Do you want to print these dicts like you expected ?

You can write :

for ent in NACLS:
    set_filter = {'Name': 'network-acl-id', 'Values': [naclID]}
    print(set_filter,end=",")
    set_filter.update({'Name': 'entry.rule-number', 'Values': [str(ent[0])]})
    print(set_filter,end="\n")

CodePudding user response:

The expected output example isn't a valid dictionary:

{'Name': 'network-acl-id', 'Values': ['acl-08128a2540eb']}, {'Name': 'entry.rule-number', 'Values': ['50']}

Reformatted:

{
    'Name': 'network-acl-id',
    'Values': [ 'acl-08128a2540eb' ]
},
{
    'Name': 'entry.rule-number',
    'Values': [ '50' ]
}

To create a list of dictionaries:

for ent in NACLS:
    set_filter = [{'Name': 'network-acl-id', 'Values': [naclID]}]
    #            ^ note square brackets                         ^
    print(set_filter)
    set_filter.append({'Name': 'entry.rule-number', 'Values': [str(ent[0])]})
    print(set_filter)

Or to put them all into a list:

set_filter = []
for ent in NACLS:
    set_filter.append({'Name': 'network-acl-id', 'Values': [naclID]})
    print(set_filter)
    set_filter.append({'Name': 'entry.rule-number', 'Values': [str(ent[0])]})
    print(set_filter)

CodePudding user response:

I don't understand much about your purpose of the list of dictionary, the suggested code

set_filters = []
for ent in NACLS:
    set_filters.append({'Name': 'network-acl-id', 'Values': [naclID]})
    set_filters.append({'Name': 'entry.rule-number', 'Values': [str(ent[0])]})

is close to your idea. However, the two dictionaries you tried to gather in the list can be combined into just one:

set_filters = []
for ent in NACLS:
    set_filters.append(
        {
            'network-acl-id': [naclID],
            'entry.rule-number': [str(ent[0])]
        }
    )

From here, you can make it shorter as

set_filters = [
    { 'network-acl-id': [naclID], 'entry.rule-number': [str(ent[0])] } for ent in NACLS
]
  • Related