Materials
Materials

Reputation: 149

KeyError: 0 while iterating over keys of a dictionary in a dictionary

I want to delete the person of each team with the least score. But I always got an KeyError: 0

Players = {'An': {'Team ':'Red','Score':6 }, 'Jef': {'Team':'Blue','Score': 7 },'Jos': {'Team':'Red','Score':5 }}
names= Players.keys()
for i in range (len(names)):
      if Players[i]["Team"] == Players[i+1]["Team"] :
          if Players[i]['Score'] > PLayers[i+1]['Score']:
            Players.remove(Players[i+1])

Has anyone an idea which mistake I make? (For the record,I know this code is inefficient as hell, but for this project I can't use most Python tools or a list comprehension for example )

Upvotes: 1

Views: 5059

Answers (6)

HiroshiFuu
HiroshiFuu

Reputation: 311

You should do something as this:

Players = {'An': {'Team':'Red','Score':6 }, 'Jef': {'Team':'Blue','Score':7 },'Jos': {'Team':'Red','Score':5 }}
names = Players.keys()
player_to_remove = []
for i in range(0, len(names) - 1):
    for j in range(i + 1, len(names)):
        if Players[names[i]]['Team'] == Players[names[j]]['Team'] :
            if Players[names[i]]['Score'] < Players[names[j]]['Score']:
                player_to_remove.append(names[i])
            if Players[names[i]]['Score'] > Players[names[j]]['Score']:
                player_to_remove.append(names[j])
for key in player_to_remove:
    del Players[key]

The print our of Players is: {'Jef': {'Score': 7, 'Team': 'Blue'}, 'An': {'Score': 6, 'Team': 'Red'}}
Joe has been removed.

Upvotes: 2

ronakg
ronakg

Reputation: 4212

Your problem is you are looping by index, which is not there with dictionaries. You need to loop by keys and then do whatever processing you want to do.

for pname, pinfo in Players.iteritems()

Upvotes: 1

Craig Burgler
Craig Burgler

Reputation: 1779

You are trying to index the dict Players as if it were a list. Use:

for player in Players:

Also please do not capitalize Players since it makes it look like a class.

Upvotes: 0

no coder
no coder

Reputation: 2290

You don't have specified key in those dictionary.

Players = {'An': {'Team ':'Red','Score':6 }, 'Jef': {'Team':'Blue','Score': 7 },'Jos': {'Team':'Red','Score':5 }}
names= Players.keys()
for i in range (len(names)): # range will give you the integer, not the key in Players dict.
      if Players[i]["Team"] == Players[i+1]["Team"] : # eg: for 1st loop you will get Players[0]["Team"] is not present in Players dict, will throw the key error.
          if Players[i]['Score'] > PLayers[i+1]['Score']:
            Players.remove(Players[i+1])

Upvotes: 0

Spirine
Spirine

Reputation: 1877

In the first for loop, the index is an integer. Then, you're trying to access Players[i]: is impossible, because the keys of Players are only strings. In particular, calling Players[0] leads to an error: the '0' key doesn't exist. The KeyError comes from this.

Instead, you should use for key in Players.keys():

Upvotes: 0

Adam Smith
Adam Smith

Reputation: 54173

for i in range(len(names)) does 0, 1, 2, 3, ...

Players doesn't have any of those keys.

Not to mention you're trying to create some kind of an order in a dictionary, which is by definition an orderless data structure. Down this road lies madness.

Upvotes: 1

Related Questions