简体   繁体   中英

Python - How to add multiple keys and values in dictionary

I am trying to add the dictionary values and keys dynamically.

Example:

First i will have the dictionary as:

dict1 = {'ID':2048, 'Name':'john', 'Father Name':'David'}

Then in the next iteration dictionary should be updated with below:

dict1 = ({'ID':2048, 'Name':'john', 'Father Name':'David'}, {'ID':2054, 'Name':'steve', 'Father Name':'Bird'})

Then the next iteration will be

dict1 = ({'ID':2048, 'Name':'john', 'Father Name':'David'}, {'ID':2054, 'Name':'steve', 'Father Name':'Bird'}, {'ID':2046, 'Name':'Calvin', 'Father Name':'Adam'})

And so on.....

Then i have to sort the dict1 as per the ID as below:

dict1 = ({'ID':2046, 'Name':'Calvin', 'Father Name':'Adam'}, {'ID':2048, 'Name':'john', 'Father Name':'david'}, {'ID':2054, 'Name':'steve', 'Father Name':'Bird'})

I have tried with below code:

dict1 = {}

## Here Candidate_ID, Candidate_Name and Candidate_Father_Name will be updated as per the script flow.
dict1.update({'ID': Candidate_ID, 'Name': Candidate_Name, 'Father Name' : Candidate_Father_Name)

Please suggest how to update the keys in dictionary with same values(ID, Name, Father Name), And then to sort the dictionary with value "ID".

You want to collect this in a list of dictionaries since you are looking for each dictionary to be a collection of information that hold the same keys. Dictionaries can only hold unique keys, so in order to be able to have dictionaries the way you are looking to structure it, you need a data structure that will support this. The list, here, is a good candidate.

So, you want something like this:

data = []
user_data = {'ID':2048, 'Name':'john', 'Father Name':'David'}
data.append(user_data)
new_user_data = {'ID':2049, 'Name':'bob', 'Father Name':'McBobsalot'}
data.append(new_user_data)

output of data:

data = [{'ID':2048, 'Name':'john', 'Father Name':'David'}, {'ID':2049, 'Name':'bob', 'Father Name':'McBobsalot'}]

An alternative way to look at this, is possibly to still have a dictionary of dictionaries, but you would need some re-structuring. Consider, having the 'ID' itself being a key for each dictionary, and the value of said key would be the dictionary of interest. To help illustrate this, take a look at how it would look like:

data = {
    2048: {'ID':2048, 'Name':'john', 'Father Name':'David'}, 
    2049: {'ID':2049, 'Name':'bob', 'Father Name':'McBobsalot'}
}

A very simple example of adding to the dictionary would be something like this:

data = {}
new_data = {'ID':2048, 'Name':'john', 'Father Name':'David'}
data[new_data['ID']] = new_data

A very simple method to help support this dynamically where you just pass your dictionary and data you want to add:

def add_data(your_dict, new_data):
    your_dict[new_data['ID']] = new_data
    return your_dict

I believe dictionaries only support one key and one value.

Check this out: Dictionaries - Learn Python

What you could do is to transform your value in a list or an array.

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