How to add data to dictionary from an array conditionally

Question:

I have dictionaries in a list that already have some data and I want to add a vin number to each brand in these dictionaries.

my_brand_dict = [
    {"key": {"Brand": "Tesla", "Date": "20203"}, "Total": 56},
    {"key": {"Brand": "Tesla", "Date": "20207"}, "Total": 88},
    {"key": {"Brand": "Audi", "Date": "202014"}, "Total": 79},
    {"key": {"Brand": "Mercedes", "Date": "20201"}, "Total": 49},
]

my_vins = ["f60a0a", "#2019c0", "#a81b1b", "#468650", "#21248a", "#ff7a00"]

When Brand is Tesla add '#468650'
When Brand is Mercedes add '#2019c0'
When Brand is Toyota add '#21248a'
When Brand is Audi add '#ff7a00'

My expected output:

my_brand_dict = [
    {"key": {"Brand": "Tesla", "Date": "20203"}, "Total": 56, "my_vin": "#468650"},
    {"key": {"Brand": "Toyota", "Date": "20207"}, "Total": 88, "my_vin": "#21248a"},
    {"key": {"Brand": "Audi", "Date": "202014"}, "Total": 79, "my_vin": "#ff7a00"},
    {"key": {"Brand": "Mercedes", "Date": "20201"}, "Total": 49, "my_vin": "#2019c0"},
]

Couldn’t find anything that matches what I want to achieve

Conditionally add values to dictionary

Asked By: Alexander

||

Answers:

my_brand_dict = [{'key': {'Brand': 'Tesla', 'Date': '20203'}, 'Total': 56}, {'key': {'Brand': 'Tesla', 'Date': '20207'}, 'Total': 88},
                 {'key': {'Brand': 'Audi', 'Date': '202014'}, 'Total': 79}, {'key': {'Brand': 'Mercedes', 'Date': '20201'}, 'Total': 49}]


my_vins = ['f60a0a', '#2019c0', '#a81b1b', '#468650', '#21248a', '#ff7a00']

# When Brand Tesla add '#468650'
# When Brand Mercedes add '#2019c0'
# When Brand Toyota add '#21248a'
# When Brand Audi add '#ff7a00'

for item in my_brand_dict:
    if item['key']['Brand'] == 'Tesla':
        item['my_vin'] = '#468650'
    elif item['key']['Brand'] == 'Mercedes':
        item['my_vin'] = '#2019c0'
    elif item['key']['Brand'] == 'Toyota':
        item['my_vin'] = '#21248a'
    elif item['key']['Brand'] == 'Audi':
        item['my_vin'] = '#ff7a00'

print(my_brand_dict)

This code works for me

Answered By: 0xRyN

You can define a dict base Brand & my_vins. Then use the defined dict and change value in-place in the my_brand_dict like the below.

my_vins_dct = {'Tesla' : '#468650', 
               'Mercedes' : '#2019c0', 
               'Toyota' : '#21248a', 
               'Audi' : '#ff7a00'}

my_brand_dict = [
    {"key": {"Brand": "Tesla", "Date": "20203"}, "Total": 56},
    {"key": {"Brand": "Tesla", "Date": "20207"}, "Total": 88},
    {"key": {"Brand": "Audi", "Date": "202014"}, "Total": 79},
    {"key": {"Brand": "Mercedes", "Date": "20201"}, "Total": 49},
    {"key": {"Brand": "xxxx", "Date": "20201"}, "Total": 49},    
]

for dct in my_brand_dict:
    # First approach try/except and 'continue'
    try :
        dct['my_vin'] = my_vins_dct[dct['key']['Brand']]
    except KeyError:
        continue

    # Second approach for adding 'Not Found'
    # dct['my_vin'] = my_vins_dct.get(dct['key']['Brand'], 'Brand Not Found')


print(my_brand_dict)

Output:

[
    {'key': {'Brand': 'Tesla', 'Date': '20203'}, 'Total': 56, 'my_vin': '#468650'}, 
    {'key': {'Brand': 'Tesla', 'Date': '20207'}, 'Total': 88, 'my_vin': '#468650'}, 
    {'key': {'Brand': 'Audi', 'Date': '202014'}, 'Total': 79, 'my_vin': '#ff7a00'}, 
    {'key': {'Brand': 'Mercedes', 'Date': '20201'}, 'Total': 49, 'my_vin': '#2019c0'}, 
    {'key': {'Brand': 'xxxx', 'Date': '20201'}, 'Total': 49}
]

# Output Second approach
# [
#     {'key': {'Brand': 'Tesla', 'Date': '20203'}, 'Total': 56, 'my_vin': '#468650'}, 
#     {'key': {'Brand': 'Tesla', 'Date': '20207'}, 'Total': 88, 'my_vin': '#468650'}, 
#     {'key': {'Brand': 'Audi', 'Date': '202014'}, 'Total': 79, 'my_vin': '#ff7a00'}, 
#     {'key': {'Brand': 'Mercedes', 'Date': '20201'}, 'Total': 49, 'my_vin': '#2019c0'},
#     {'key': {'Brand': 'xxxx', 'Date': '20201'}, 'Total': 49, 'my_vin': 'Brand Not Found'}
# ]
Answered By: I'mahdi

I would suggest using a dictionary instead of a list for your my_vins so that it maps brands to vins. This way you can easily get corresponding vin.

my_brand_dict = [
    {"key": {"Brand": "Tesla", "Date": "20203"}, "Total": 56},
    {"key": {"Brand": "Tesla", "Date": "20207"}, "Total": 88},
    {"key": {"Brand": "Audi", "Date": "202014"}, "Total": 79},
    {"key": {"Brand": "Mercedes", "Date": "20201"}, "Total": 49},
]

my_vins = {
    "Mercedes": "#2019c0",
    "Tesla": "#468650",
    "Toyota": "#21248a",
    "Audi": "#ff7a00",
}

for d in my_brand_dict:
    brand = d["key"]["Brand"]
    vin = my_vins[brand]
    d["my_vin"] = vin

print(my_brand_dict)

Then take care of what should happen if a brand doesn’t have a vin, You can raise exception or assign a default value.

Answered By: S.B
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.