0

I have the following: list of dictionaries and a dictionary where the key is the value of key "userid" in my list of dictionaries. I want to take the data within dictionary extra and add it to each dictionary based on if the userid matches. Below is my sample data and what I have tried.

data = [{'date':'2021-01-01', 
         'userid': 'ABC123', 
         'name': 'John Smith', 
         'age': 15}, 
         {'date':'2021-01-10', 
         'userid': 'DEF123', 
         'name': 'Jane Doe', 
         'age': 19}]

extra = {'ABC123' : {"favorite sport": "basketball",
                     "favorite color": "red"},
         'DEF123': {"favorite sport": "soccer",
                     "favorite color": "yellow"}}  

def combine(data, extra): 
    data_new = data.copy()
    extra_new = extra.copy() 
    ids = list(extra_new.keys())

    output = []
    for value in data_new:
        value.update(extra_new)
        output.append(value)
    return output      

The above results in

output = [{'date':'2021-01-01', 
         'userid': 'ABC123', 
         'name': 'John Smith', 
         'age': 15,
         'ABC123' : {"favorite sport": "basketball",
                     "favorite color": "red"},
         'DEF123': {"favorite sport": "soccer",
                     "favorite color": "yellow"}} 
         {'date':'2021-01-10', 
         'userid': 'DEF123', 
         'name': 'Jane Doe', 
         'age': 19, 
         'ABC123' : {"favorite sport": "basketball",
                     "favorite color": "red"},
         'DEF123': {"favorite sport": "soccer",
                     "favorite color": "yellow"}}]

What I want is:

output = [{'date':'2021-01-01', 
         'userid': 'ABC123', 
         'name': 'John Smith', 
         'age': 15,
         "favorite sport": "basketball",
         "favorite color": "red"} 
         {'date':'2021-01-10', 
         'userid': 'DEF123', 
         'name': 'Jane Doe', 
         'age': 19, 
         "favorite sport": "soccer",
         "favorite color": "yellow"}]
deepvalue
  • 25
  • 4

2 Answers2

0

It seems I have come up with a quite simple solution if I understood Your problem correctly.
The only thing I've edited is the combine function and it looks like this:

def combine(data, extra):
    data_new = data.copy()

    for i in range(len(data_new)):
        if data_new[i]['userid'] in extra:
            data_new[i].update(extra[data_new[i]['userid']])
    return data_new

The main two issues in Your code are:

  • You didn't check for dictionary key correspondence(although You stored the keys in a variable)
  • You didn't specify any dictionary of data, in which the extra values should be added

Also, not a big thing, but it seems unnecessary to copy the extra dictionary since you're not changing anything about it in this particular function.

P.S. I'm very new to answering, so I really hope this helps

Devkey
  • 51
  • 1
  • 5
0

this one is uglier


#!/usr/bin/env python3
# -*- coding: utf-8 -*-



data = [{'date':'2021-01-01', 
         'userid': 'ABC123', 
         'name': 'John Smith', 
         'age': 15}, 
         {'date':'2021-01-10', 
         'userid': 'DEF123', 
         'name': 'Jane Doe', 
         'age': 19}]

extra = {'ABC123' : {"favorite sport": "basketball",
                     "favorite color": "red"},
         'DEF123': {"favorite sport": "soccer",
                     "favorite color": "yellow"}}  

def combine(data, extra): 
    data_new = data.copy()
    ids = list(extra.keys())

    for value in data_new:
        indexx=data_new.index(value)
        for valuez in value:
            if value[valuez] in ids:
                z={**data[indexx],**(extra[value[valuez]])}
                data_new[indexx]=z
    return data_new

pippo = combine(data, extra)

  
print(pippo)

never the less returns

[{'date': '2021-01-01', 'userid': 'ABC123', 'name': 'John Smith', 'age': 15, 'favorite sport': 'basketball', 'favorite color': 'red'}, {'date': '2021-01-10', 'userid': 'DEF123', 'name': 'Jane Doe', 'age': 19, 'favorite sport': 'soccer', 'favorite color': 'yellow'}]

using dictionary merge, see: How do I merge two dictionaries in a single expression in Python (taking union of dictionaries

and used

indexx=data_new.index(value)

because forgot about len(list) and had troubles accessing list by index ;-)

pippo1980
  • 2,181
  • 3
  • 14
  • 30