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?

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)
    – AChampion
    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.
    – AChampion
    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?)
    – NPE
    Commented Jul 20, 2017 at 1:54

2 Answers 2

2

For Py>3.5:

It's easy. Just enter:

d = {**b, **c}
7
  • I only know how to use Py3. :p
    – Root
    Commented Jul 20, 2017 at 1:34
  • @Root You should know that there are solutions that work on python3 as well as python2.
    – cs95
    Commented Jul 20, 2017 at 1:35
  • I would note that in your answer - also note this in only Py>3.5.
    – AChampion
    Commented Jul 20, 2017 at 1:36
  • Good advice. I will add it.
    – Root
    Commented Jul 20, 2017 at 1:40
  • @root, have you run this with the sample data? Does this give the answer OP expects?
    – Robᵩ
    Commented Jul 20, 2017 at 2:03
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)
2
  • 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
    Commented Jul 20, 2017 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ᵩ
    Commented Jul 20, 2017 at 16:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.