Assign a value of one key to another key in if statement within for loop Python

Viewed 1309

I have a list of dictionaries in Python. I loop through every dictionary and check if the given key exists. If it does exist I check if the value of this key is empty, if it's empty then I need to save another key's value in that empty key. Can't figure out how to assign a value from one key to another...

My data looks like this:

my_dicts = [{"Id": "123", "Campaign_Date": "2020-06-30", "Another_Date": "2020-07-01"}, {"Id": "125", "Campaign_Date": "", "Another_Date": "2020-07-01"}]

The output should like this:

my_dicts = [{"Id": "123", "Campaign_Date": "2020-06-30", "Another_Date": "2020-07-01"}, {"Id": "125", "Campaign_Date": "2020-07-01", "Another_Date": "2020-07-01"}]

I have tried the following:

for my_dict in my_dicts:
    for key in my_dict.items():
        campaign_key = 'Campaign_Date'
        if key == campaign_key:
            if value == "":
                value = value["Another_Date"] 
        else:
            continue
3 Answers

Just in case if you do not want wish to update original dictionary and at same time make necessary correction into new dictionary.

import copy
#my_dicts = [{"Id": "123", "Campaign_Date": "2020-06-30", "Another_Date": "2020-07-01"}, {"Id": "125", "Campaign_Date": "", "Another_Date": "2020-07-01"}]
# New input
my_dicts = [{"Id": "123", "Campaign_Date": "2020-06-30", "Another_Date": "2020-07-01"}, {"Id": "125", "Campaign_Date": "", "Another_Date": "2020-07-01"}, {"Id": "126", "Another_Date": "2020-07-01"}]

my_new_list = copy.deepcopy(my_dicts)

campaign_key = 'Campaign_Date'
for index, my_dict in enumerate(my_new_list):
    # check if "campaign_key" key exists
    if campaign_key in my_dict.keys():
        # Check for empty or null value for key "campaign_key"
        if my_dict[campaign_key] in ["", None]:
            my_dict['Campaign_Date'] = my_dict["Another_Date"]
    else:
        # if key "campaign_key" doesn't exist delete that item from list
        my_new_list.pop(index)

print(my_dicts)
print(my_new_list)
expected_output = [{'Id': '123', 'Campaign_Date': '2020-06-30', 'Another_Date': '2020-07-01'}, {'Id': '125', 'Campaign_Date': '2020-07-01', 'Another_Date': '2020-07-01'}]
if my_new_list == expected_output:
    print("Success")

Output:

[{'Id': '123', 'Campaign_Date': '2020-06-30', 'Another_Date': '2020-07-01'}, {'Id': '125', 'Campaign_Date': '', 'Another_Date': '2020-07-01'}, {'Id': '126', 'Another_Date': '2020-07-01'}]
[{'Id': '123', 'Campaign_Date': '2020-06-30', 'Another_Date': '2020-07-01'}, {'Id': '125', 'Campaign_Date': '2020-07-01', 'Another_Date': '2020-07-01'}]
Success
for my_dict in my_dicts:
    for key in my_dict.items():
        campaign_key = 'Campaign_Date'
        if key == campaign_key:
            if value = "":
                my_dict['Campaign_Date'] = my_dict["Another_Date"] 
        else:
            continue

Not sure if it's in the code you're running but there is a bug in the code you gave.

for my_dict in my_dicts:
    for key in my_dict.items():
        campaign_key = 'Campaign_Date'
        if key == campaign_key:
            if value = "": # This should be == otherwise you're assigning value to ""
                value = value["Another_Date"] # value["Another_Date"] Doesn't exist
                # Also this won't put the value into the dictionary
        else:
            continue
for my_dict in my_dicts:
    for key, value in my_dict.items():
        if key == 'Campaign_Date' and value == "":
           value = my_dict["Another_Date"]

Please note that this will not update your dictionary. To do that replace value = my_dict["Another_Date"] with my_dict.update({'Campaign_Date':my_dict["Another_Date"]})

Related