Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add data to dictionary from an array conditionally

I have dictionary in a list that has already some data in it and I want to add a vin number to each brand in this list.

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'

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Conditionally add values to dictionary

>Solution :

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading