简体   繁体   中英

How to change specific key values with array values in dictionary using loop?

I have 1 dictionary and 1 array. My task is to run through dictionary and create 5 separete dictionaries where key "email" will be replaced with array values. All my attempts to create loops just use the last array's value so there is only one dict. How to loop it correctly to solve it

data = {
        "company": "Company",
        "phone": "+1111111",
        "email": "mailtest.unconfirm@yahoo.com",
        "password1": "defaultpassword",
        "password2": "defaultpassword",
        "terms_agree": True,
        "first_name": "TestUser",
        "last_name": "One"

}

emails_list = ['mailtest.unconfirm@yahoo.com',
        'mailtest.unconfirm@gmail.com',
        'mailtest.unconfirm@live.com',
        'mailtest.unconfirm@outlook.com',
        'mailtest.unconfirm@icloud.com'
        ]

You can do this in a single line with python! See below

result = [{**data,**{"email": val}} for val in emails_list]

This creates a list of N dicts, as per length of your emails. resulting in

[
    {
        "company": "Company",
        "phone": "+1111111",
        "email": "mailtest.unconfirm@yahoo.com",
        "password1": "defaultpassword",
        "password2": "defaultpassword",
        "terms_agree": true,
        "first_name": "TestUser",
        "last_name": "One"
    },
    {
        "company": "Company",
        "phone": "+1111111",
        "email": "mailtest.unconfirm@gmail.com",
        "password1": "defaultpassword",
        "password2": "defaultpassword",
        "terms_agree": true,
        "first_name": "TestUser",
        "last_name": "One"
    },
    {
        "company": "Company",
        "phone": "+1111111",
        "email": "mailtest.unconfirm@live.com",
        "password1": "defaultpassword",
        "password2": "defaultpassword",
        "terms_agree": true,
        "first_name": "TestUser",
        "last_name": "One"
    },
    {
        "company": "Company",
        "phone": "+1111111",
        "email": "mailtest.unconfirm@outlook.com",
        "password1": "defaultpassword",
        "password2": "defaultpassword",
        "terms_agree": true,
        "first_name": "TestUser",
        "last_name": "One"
    },
    {
        "company": "Company",
        "phone": "+1111111",
        "email": "mailtest.unconfirm@icloud.com",
        "password1": "defaultpassword",
        "password2": "defaultpassword",
        "terms_agree": true,
        "first_name": "TestUser",
        "last_name": "One"
    }
]

I might be misunderstanding what you want, but can't you just loop through the array and individually set the dictionary's email and copy that? Example below:

emails_dict_ls = []
for email in emails_list:
     data_copy = data.copy()
     data_copy["email"] = email
     emails_dict_ls.append(data_copy)
print(emails_dict_ls)

This will print out the following

[{'company': 'Company', 'phone': '+1111111', 'email': 'mailtest.unconfirm@yahoo.com', 'password1': 'defaultpassword', 'password2': 'defaultpassword', 'terms_agree': True, 'first_name': 'TestUser', 'last_name': 'One'},
{'company': 'Company', 'phone': '+1111111', 'email': 'mailtest.unconfirm@gmail.com', 'password1': 'defaultpassword', 'password2': 'defaultpassword', 'terms_agree': True, 'first_name': 'TestUser', 'last_name': 'One'},
{'company': 'Company', 'phone': '+1111111', 'email': 'mailtest.unconfirm@live.com', 'password1': 'defaultpassword', 'password2': 'defaultpassword', 'terms_agree': True, 'first_name': 'TestUser', 'last_name': 'One'},
{'company': 'Company', 'phone': '+1111111', 'email': 'mailtest.unconfirm@outlook.com', 'password1': 'defaultpassword', 'password2': 'defaultpassword', 'terms_agree': True, 'first_name': 'TestUser', 'last_name': 'One'},
{'company': 'Company', 'phone': '+1111111', 'email': 'mailtest.unconfirm@icloud.com', 'password1': 'defaultpassword', 'password2': 'defaultpassword', 'terms_agree': True, 'first_name': 'TestUser', 'last_name': 'One'}]

You need to make a deep copy:

import copy

def new_dic(email,data_dic):
    new_dic = copy.deepcopy(data_dic); new_dic["email"] = email
    return(new_dic)

data = {
        "company": "Company",
        "phone": "+1111111",
        "email": "mailtest.unconfirm@yahoo.com",
        "password1": "defaultpassword",
        "password2": "defaultpassword",
        "terms_agree": True,
        "first_name": "TestUser",
        "last_name": "One"}

emails_list = ['mailtest.unconfirm@yahoo.com',
        'mailtest.unconfirm@gmail.com',
        'mailtest.unconfirm@live.com',
        'mailtest.unconfirm@outlook.com',
        'mailtest.unconfirm@icloud.com'
        ]

dic_list = []
for x in range(0,len(emails_list)):
    dic_list.append(new_dic(emails_list[x],data))

print(dic_list[0]['email'])
'mailtest.unconfirm@yahoo.com'
print(dic_list[-1]['email'])
'mailtest.unconfirm@icloud.com'

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