1

i have these two dictionaries and i would like to add the data dict1 in dict2. how can i do that with python ?

dict1 = {
    213688169:
        {'stationCode': '6003',
         'station_id': 213688169,
         'num_bikes_available': 10,
         'numBikesAvailable': 10,
         'num_bikes_available_types': [{'mechanical': 10}, {'ebike': 0}],
         'num_docks_available': 10,
         'numDocksAvailable': 10,
         'is_installed': 1,
         'is_returning': 1,
         'is_renting': 1,
         'last_reported': 1619207955}
}

dict2 = {
    213688169:
        {'station_id': 213688169,
         'name': 'Benjamin Godard - Victor Hugo',
         'lat': 48.865983,
         'lon': 2.275725,
         'capacity': 35,
         'stationCode': '16107'}
}

i tried this but it's too long and complicated :

donnees=[]
for i in stations:
    for j in velib_description :
         if i['station_id'] == j['station_id']:
                List={}
                List['name'] = i['name']
                List['lat'] = i['lat']
                List['lon'] = i['lon']
                List['capacity'] = i['capacity']
                List['num_bikes_available'] = j['num_bikes_available']
                List['num_bikes_available_types'] = j['num_bikes_available_types']
                List['last_reported'] = j['last_reported']
                donnees.append(List)

I want to add in dict_2 = {num_bikes_available', 'num_bikes_available_types', last_reported': 1619207955 }

thank you

Selcuk
  • 57,004
  • 12
  • 102
  • 110
amaranaitsaidi
  • 101
  • 1
  • 9
  • "i tried this but it's too long and complicated"--also it doesn't work i.e. stations and velib_description are undefined. – DarrylG Apr 25 '21 at 11:47

2 Answers2

0

You can use a dictionary comprehension with a dictionary merge trick:

dict3 = {k: {**v, **dict2[k]} for k, v in dict1.items()}

or for Python 3.9 and later:

dict3 = {k: v | dict2[k] for k, v in dict1.items()}
Selcuk
  • 57,004
  • 12
  • 102
  • 110
0

Perhaps you want dict.update?

dict1 = {
    213688169: {
        "stationCode": "6003",
        "station_id": 213688169,
        "num_bikes_available": 10,
        "numBikesAvailable": 10,
        "num_bikes_available_types": [{"mechanical": 10}, {"ebike": 0}],
        "num_docks_available": 10,
        "numDocksAvailable": 10,
        "is_installed": 1,
        "is_returning": 1,
        "is_renting": 1,
        "last_reported": 1619207955,
    }
}

dict2 = {
    213688169: {
        "station_id": 213688169,
        "name": "Benjamin Godard - Victor Hugo",
        "lat": 48.865983,
        "lon": 2.275725,
        "capacity": 35,
        "stationCode": "16107",
    }
}

dict1_item = dict1[213688169]
dict2[213688169].update({k: v for k, v in dict1_item.items() if k not in dict2})

dict2

This prints

{213688169: {'station_id': 213688169,
  'name': 'Benjamin Godard - Victor Hugo',
  'lat': 48.865983,
  'lon': 2.275725,
  'capacity': 35,
  'stationCode': '6003',
  'num_bikes_available': 10,
  'numBikesAvailable': 10,
  'num_bikes_available_types': [{'mechanical': 10}, {'ebike': 0}],
  'num_docks_available': 10,
  'numDocksAvailable': 10,
  'is_installed': 1,
  'is_returning': 1,
  'is_renting': 1,
  'last_reported': 1619207955}}

We're using a comprehension to get all the key/values from the first dictionary that aren't in the second, then updating the second dictionary. We have to do a little munging with the 213688169 key because your dictionary is nested.

user1717828
  • 7,122
  • 8
  • 34
  • 59