Home > Enterprise >  Remove uncommon keys from two dictionaries in Python
Remove uncommon keys from two dictionaries in Python

Time:03-12

I'm writing a program in Python that is meant to remove uncommon keys and their associated values from two dictionaries. For example, given the following dictionaries:

games =
{ 'WYO': {2018: (41, 19)}, 
'SJSU': {2018: (31, 0)}, 
'EWU': {2018: (59, 24)}, 
'USC': {2018: (36, 39), 2020: (13, 38), 2021: (14, 45)}, 
'UTAH': {2018: (28, 24), 2019: (13, 38), 2020: (28, 45), 2021: (13, 24)}, 
'ORST': {2018: (56, 37), 2019: (54, 53), 2020: (38, 28), 2021: (31, 24)}, 
'ORE': {2018: (34, 20), 2019: (35, 37), 2020: (29, 43), 2021: (24, 38)}, 
'STAN': {2018: (41, 38), 2019: (49, 22), 2021: (34, 31)}, 
'CAL': {2018: (19, 13), 2019: (20, 33), 2021: (21, 6)}, 
'COLO': {2018: (31, 7), 2019: (41, 10)}, 
'ARIZ': {2018: (69, 28), 2021: (44, 18)}, 
'WASH': {2018: (15, 28), 2019: (13, 31), 2021: (40, 13)}, 
'ISU': {2018: (28, 26)}, 
'NMSU': {2019: (58, 7)}, 
'UNCO': {2019: (59, 17)}, 
'HOU': {2019: (31, 24)}, 
'UCLA': {2019: (63, 67)}, 
'ASU': {2019: (34, 38), 2021: (34, 21)}, 
'AFA': {2019: (21, 31)}, 
'USU': {2021: (23, 26)}, 
'PORT ST.': {2021: (44, 24)}, 
'BYU': {2021: (19, 21)}, 
'CMU': {2021: (21, 24)} }

and

comp_dict = {2018:(0,0), 2019:(0,0), 2020:(0,0), 2021:(0,0)}

I want to remove all the keys and their associated values from games that don't have all the same keys as comp_dict. The result would be:

{ 'UTAH': {2018: (28, 24), 2019: (13, 38), 2020: (28, 45), 2021: (13, 24)}, 
'ORST': {2018: (56, 37), 2019: (54, 53), 2020: (38, 28), 2021: (31, 24)}, 
'ORE': {2018: (34, 20), 2019: (35, 37), 2020: (29, 43), 2021: (24, 38)} }

I'm trying to create a copy of games called commonteams, that I can remove the uncommon elements from. This is what I have so far:

def common_teams(data):
     comp_dict = {2018:(0,0), 2019:(0,0), 2020:(0,0), 2021:(0,0)}
     games = data
     commonteams = games
     for team,log in games.items():
          if  log.keys() != comp_dict.keys():
               commonteams.pop(team)
     return commonteams

CodePudding user response:

In a case like this, it's usually best to copy over the things you want into a new object, rather than modify the one you have, especially when you are iterating through the old object.

def common_teams(data):
     newdict = {}
     for team,log in games.items():
          if  log.keys() == comp_dict.keys():
               newdict[team] = log
     return newdict

print(common_teams(games))

CodePudding user response:

Here's one way using dict.pop:

for k in list(games):
    if games[k].keys() != comp_dict.keys():
        games.pop(k)

Output:

{'UTAH': {2018: (28, 24), 2019: (13, 38), 2020: (28, 45), 2021: (13, 24)},
 'ORST': {2018: (56, 37), 2019: (54, 53), 2020: (38, 28), 2021: (31, 24)},
 'ORE': {2018: (34, 20), 2019: (35, 37), 2020: (29, 43), 2021: (24, 38)}}

Note that dict.pop works here only because we're iterating over a copy of games.keys() here.

CodePudding user response:

Well... I prefer dic comprehension...

d = { k:v for k, v in games.items() if v.keys() == comp_dict.keys() }

If you have to print as your output example then you can use pprint()..

  • Related