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
538k77 gold badges594 silver badges746 bronze badges
asked Jul 20, 2017 at 1:26
6
  • 6
    Possible duplicate of How to merge two Python dictionaries in a single expression? Commented Jul 20, 2017 at 1:29
  • 1
    If you don't care if this done in a single line (why would you) then d = dict(b); d.update(c) Commented Jul 20, 2017 at 1:29
  • @AChampion Why did you cast b to a dict? Commented Jul 20, 2017 at 1:30
  • @CoryMadden It's not really casting, it is creating a copy of b, so the update() doesn't change b. Commented Jul 20, 2017 at 1:31
  • 1
    Please could you make sure that the code in your question is syntactically valid. (Presumably, data, station_id etc are strings?) Commented Jul 20, 2017 at 1:54

2 Answers 2

2

For Py>3.5:

It's easy. Just enter:

d = {**b, **c}
answered Jul 20, 2017 at 1:28
Sign up to request clarification or add additional context in comments.

7 Comments

I only know how to use Py3. :p
@Root You should know that there are solutions that work on python3 as well as python2.
I would note that in your answer - also note this in only Py>3.5.
Good advice. I will add it.
@root, have you run this with the sample data? Does this give the answer OP expects?
|
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)
answered Jul 20, 2017 at 2:04

2 Comments

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??
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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.