All you need is:
# to change already existing dict
players = {players['player_id']:{key:value for key,value in players.items() if key != 'player_id'}}
# make dict from lists a and b
z = {b[a=='player_id']: {key:value for key,value in zip(a,b) if key !='player_id'}}
# group both
players={**players,**z}
Details Below:
First you will need to change your already made dict to the desired output type like this:
players = {players['player_id']:{key:value for key,value in players.items() if key != 'player_id'}}
print(players)
will output:
{4: {'gyms_visited': [],
'player_name': 'cynthia',
'player_pokemon': {},
'time_played': 30.9}}
then you take your two lists a
and b
and make an additional dict out of them like this:
z = {b[a=='player_id']: {key:value for key,value in zip(a,b) if key !='player_id'}}
print(z)
will output:
{2: {'player_name':
'teri', 'time_played': 22.2,
'player_pokemon': {},
'gyms_visited': []}}
and Finally, you just put them in the same dict as follows:
players={**players,**z}
print(players)
output:
{4: {'gyms_visited': [],
'player_name': 'cynthia',
'player_pokemon': {},
'time_played': 30.9},
2: {'player_name': 'teri',
'time_played': 22.2,
'player_pokemon': {},
'gyms_visited': []}}
Please notice that you can do this in a loop if you want to add some more membres. For exemple consider these new lists:
a = ['player_id', 'player_name','time_played', 'player_pokemon', 'gyms_visited']
b = [6, 'new_Name', 66.6, {}, []]
by doing the same as before:
z = {b[a=='player_id']: {key:value for key,value in zip(a,b) if key !='player_id'}}
players={**players,**z}
you get as output:
{4: {'gyms_visited': [],
'player_name': 'cynthia',
'player_pokemon': {},
'time_played': 30.9},
2: {'player_name': 'teri',
'time_played': 22.2,
'player_pokemon': {},
'gyms_visited': []},
6: {'player_name': 'new_Name',
'time_played': 66.6,
'player_pokemon': {},
'gyms_visited': []}}
You can refer to these links about merging dicts and **dicts for a better understanding.
Here is also a link for the built-in zip method.