vendredi 10 avril 2020

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

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

Aucun commentaire:

Enregistrer un commentaire