简体   繁体   中英

How to update order of dictionary to last while appending

I have a list of dictionary

Primary = [{'emptype': ['Manager'], 'Designation': ['Developer']}]

If I update primary with 'projecttype': ['temp']

The new dictionary will be [{'emptype': ['Manager'], 'Designation': ['Developer'],'projecttype': ['temp'] }]

  • You can see 'projecttype': ['temp'] updated to last

If I keep on appending the values My updated key has to go to last part

Example emptype is updated with 'Associate'

[{'Designation': ['Developer'],'projecttype': ['temp'], 'emptype': ['Manager', 'Associate']}]

  • You can see emptype updated to last part.

I m using python version 3.8. So order of dictionary is preserved.

Basically if any addition in keys or adding in values those dictionary will goes to last part. How to achieve this?

primary = [{'emptype': ['Manager'], 'Designation': ['Developer'],'projecttype': ['temp']}]

x = primary[0].pop('emptype')
x.append('Associate')
primary[0].update({'emptype': x})

print(primary)

output:

[{'Designation': ['Developer'], 'projecttype': ['temp'], 'emptype': ['Manager', 'Associate']}]

or you can use a func like this:

def update_last(my_dict, key, new_value):
    x = dict.pop(key)
    x.append(new_value)
    my_dict.update({key: x})
    return my_dict

UPDATE:

@JLeno46 had a great answer, but the solution was a little off of being completely working. Here is a function that will work for both inserting a key and updating a key:

def update(key, val):
    x = Primary[0].pop(key, [])
    x.append(val)
    Primary[0][key] = x

This function can update and create new keys, and is KeyError -proof.

Notes:

  1. It uses only one key and one value. This way, it doesn't need to ignore anything.

  2. It only works for Primary . You can add a list of a dictionary parameter to make it work for all lists of dictionaries.

  3. Already said, but Primary is a list of a dictionary. You might want it to be just a dictionary. Just replace Primary[0] with Primary , and remove the [] from the declaration of Primary .


(Before Edit) Try this:

Primary = [{'emptype': ['Manager'], 'Designation': ['Developer']}]
def update(newPair):
    key = list(newPair.keys())[0]
    if key in Primary[0].keys():
        val = Primary[0].pop(key, None)
        val.append(list(newPair.values())[0])
        Primary[0][key] = val
    else:
        Primary[0][key] = [list(newPair.values())[0]]

# Test it
print(Primary[0])
update({'projecttype': 'temp'})
print(Primary[0])
update({'emptype': 'Dev'})
print(Primary[0])

Notes:

1. You can only update() 1 item at a time. Passing a list will get this:

update('projecttype': ['temp', 'helper'])

Primary is now:
[{'emptype': ['Manager'], 'Designation': ['Developer'], 'projecttype': [['temp', 'helper']]}]

which has [['temp', 'helper']] instead of ['temp', 'helper'] .

2. You must pass a dictionary with only one key. That's basically a list, but to go along with dictionaries it was made that way. If you have more than one key, only the 0th key will be used, the other ignored.

3. As @MisterMiyagi has said, Primary is a list of a dictionary . If you have just noticed this, you can remove the [] from Primary and replace all Primary[0] to Primary .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM