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": "[email protected]",
"password1": "defaultpassword",
"password2": "defaultpassword",
"terms_agree": True,
"first_name": "TestUser",
"last_name": "One"
}
emails_list = ['[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]'
]
CodePudding user response:
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": "[email protected]",
"password1": "defaultpassword",
"password2": "defaultpassword",
"terms_agree": true,
"first_name": "TestUser",
"last_name": "One"
},
{
"company": "Company",
"phone": " 1111111",
"email": "[email protected]",
"password1": "defaultpassword",
"password2": "defaultpassword",
"terms_agree": true,
"first_name": "TestUser",
"last_name": "One"
},
{
"company": "Company",
"phone": " 1111111",
"email": "[email protected]",
"password1": "defaultpassword",
"password2": "defaultpassword",
"terms_agree": true,
"first_name": "TestUser",
"last_name": "One"
},
{
"company": "Company",
"phone": " 1111111",
"email": "[email protected]",
"password1": "defaultpassword",
"password2": "defaultpassword",
"terms_agree": true,
"first_name": "TestUser",
"last_name": "One"
},
{
"company": "Company",
"phone": " 1111111",
"email": "[email protected]",
"password1": "defaultpassword",
"password2": "defaultpassword",
"terms_agree": true,
"first_name": "TestUser",
"last_name": "One"
}
]
CodePudding user response:
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': '[email protected]', 'password1': 'defaultpassword', 'password2': 'defaultpassword', 'terms_agree': True, 'first_name': 'TestUser', 'last_name': 'One'},
{'company': 'Company', 'phone': ' 1111111', 'email': '[email protected]', 'password1': 'defaultpassword', 'password2': 'defaultpassword', 'terms_agree': True, 'first_name': 'TestUser', 'last_name': 'One'},
{'company': 'Company', 'phone': ' 1111111', 'email': '[email protected]', 'password1': 'defaultpassword', 'password2': 'defaultpassword', 'terms_agree': True, 'first_name': 'TestUser', 'last_name': 'One'},
{'company': 'Company', 'phone': ' 1111111', 'email': '[email protected]', 'password1': 'defaultpassword', 'password2': 'defaultpassword', 'terms_agree': True, 'first_name': 'TestUser', 'last_name': 'One'},
{'company': 'Company', 'phone': ' 1111111', 'email': '[email protected]', 'password1': 'defaultpassword', 'password2': 'defaultpassword', 'terms_agree': True, 'first_name': 'TestUser', 'last_name': 'One'}]
CodePudding user response:
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": "[email protected]",
"password1": "defaultpassword",
"password2": "defaultpassword",
"terms_agree": True,
"first_name": "TestUser",
"last_name": "One"}
emails_list = ['[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]'
]
dic_list = []
for x in range(0,len(emails_list)):
dic_list.append(new_dic(emails_list[x],data))
print(dic_list[0]['email'])
'[email protected]'
print(dic_list[-1]['email'])
'[email protected]'