Home > Blockchain >  How to add data to dictionary from an array conditionally
How to add data to dictionary from an array conditionally

Time:09-28

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

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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

  • Related