0

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]'
        ]

3 Answers 3

1

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"
    }
]
Sign up to request clarification or add additional context in comments.

Comments

0

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'}]

Comments

0

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]'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.