0

I'm trying to add key, value to an existing dictionary from a pre-defined list. Below is an example:

# I created players dictionary : 
players = {'gyms_visited': [],
 'player_id': [4],
 'player_name': 'cynthia',
 'player_pokemon': {},
 'time_played': 30.9}

I'm trying to add the list a, b to my players dictionary that I created above:

a = ['player_id', 'player_name','time_played', 'player_pokemon', 'gyms_visited']
b = [2, 'teri', 22.2, {}, []]

Also, is there a way to make Player_id's value (4, 2) as keys to the players dictionary ( This is what I'm trying to achieve):

{4: {'gyms_visited': [],
     'player_name': 'cynthia',
     'player_pokemon': {},
     'time_played': 30.9},
 2: {'player_name':'teri',
     'time_played':22.2,
     'gyms_visited': [],
     'player_pokemon':{}}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Pary kam
  • 11
  • 3

2 Answers2

0

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.

Rayhane Mama
  • 2,374
  • 11
  • 20
0
from itertools import izip
from pprint import pprint

# Edit your existing dict
>>> players2 = {players.pop('player_id')[0]: players}
>>> pprint(players2)
{4: {'gyms_visited': [],
     'player_name': 'cynthia',
     'player_pokemon': {},
     'time_played': 30.9}}

# Add new player info
>>> players2.update({b[0]: {k: v for k, v in izip(a, b[1:])}})
>>> pprint(players2)
{2: {'gyms_visited': [],
     'player_name': 'teri',
     'player_pokemon': {},
     'time_played': 22.2},
 4: {'gyms_visited': [],
     'player_name': 'cynthia',
     'player_pokemon': {},
     'time_played': 30.9}}

Note: Whenever you're working with some iteration code, it's worth checking out the built-in itertools package. izip, for eaxample, improves over zip by returning an iterator instead of a list.

BoltzmannBrain
  • 5,082
  • 11
  • 46
  • 79
  • Happy to help; I use pokemon examples for unit tests ;) Please don't forget to select the answer with the checkmark at the left, thanks. – BoltzmannBrain Jul 03 '17 at 02:36