0

I want to merge this dictionary:

b = {data:[{station_id: 7000,
     name: "Ft. York / Capreol Crt."
     },
     {station_id: 7001,
      name: "Lower Jarvis St / The Esplanade"}
     ]}

and this one :

c = {data:[{station_id: 7000,
     num_bikes_available: 18,
     },
     {station_id: 7001,
      num_bikes_available: 4,
      }
    ]}

and get one dictionary like this:

d = {data:[{station_id: 7000,
 name: "Ft. York / Capreol Crt.",
 num_bikes_available: 18
 },
{station_id: 7001,
 name: "Lower Jarvis St / The Esplanade",                         
 num_bikes_available: 4}
]}

How can I do that?

chepner
  • 497,756
  • 71
  • 530
  • 681
kia
  • 27
  • 3
  • 6
    Possible duplicate of [How to merge two Python dictionaries in a single expression?](https://stackoverflow.com/questions/38987/how-to-merge-two-python-dictionaries-in-a-single-expression) – S.G. Harmonia Jul 20 '17 at 01:29
  • 1
    If you don't care if this done in a single line (why would you) then `d = dict(b); d.update(c)` – AChampion Jul 20 '17 at 01:29
  • @AChampion Why did you cast `b` to a dict? – Cory Madden Jul 20 '17 at 01:30
  • @CoryMadden It's not really casting, it is creating a copy of `b`, so the `update()` doesn't change `b`. – AChampion Jul 20 '17 at 01:31
  • nevermind. I reread it, I wasn't computing that `d` was a new variable – Cory Madden Jul 20 '17 at 01:32
  • 1
    Please could you make sure that the code in your question is syntactically valid. (Presumably, `data`, `station_id` etc are strings?) – NPE Jul 20 '17 at 01:54

2 Answers2

2

For Py>3.5:

It's easy. Just enter:

d = {**b, **c}
Root
  • 97
  • 1
  • 12
1

The key to this problem is picking the right data structure. Instead of b['data'] being a list, it should be a dict indexed by the merge key. The following code first converts b and c into dicts indexed by station_id, then merges those dictionaries.

Try this:

from pprint import pprint

b = {'data': [{'station_id': 7000,
     'name': "Ft. York / Capreol Crt."
     },
     {'station_id': 7001,
      'name': "Lower Jarvis St / The Esplanade"},
     {'station_id':7002,'num_bikes_available':10},
     ]}

c = {'data': [{'station_id': 7000,
     'num_bikes_available': 18,
     },
     {'station_id': 7001,
      'num_bikes_available': 4,
      }
    ]}

# First, convert B and C to a more useful format:

b1 = {item['station_id']: item for item in b['data']}
c1 = {item['station_id']: item for item in c['data']}

# Now construct D by merging the individual values
d = {'data': []}
for station_id, b_item in sorted(b1.items()):
    z = b_item.copy()
    z.update(c1.get(station_id, {}))
    d['data'].append(z)

pprint(d)
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • But what if for example in b there was also {'station_id':7002,'num_bikes_available':10} which in c there isn't. what about then?? – kia Jul 20 '17 at 13:57
  • Anytime you need an item from a dictionary, but it might not be present, use `dict.get()`. I have modified the `z.update(...)` line in response to your question. – Robᵩ Jul 20 '17 at 16:53