-3

I am just starting to learn a bit of Python. I am working with some basic dictionaries. I have a dictionary, which I make a copy of. I then take another dictionary and take the values away from my copy:

teams_goals = {'chelsea' : 2, 'man u' : 3,'Arsenal':1,'Man city':2}
print teams_goals
test_teams = {'chelsea' : 1, 'man u' : 1,'Arsenal':1}
teams_goals_copy = teams_goals.copy()

for team in test_teams:
        for j in range(test_teams[team]):
            teams_goals_copy[team]= teams_goals_copy[team]- 1
        print teams_goals_copy

This leaves me with a dictionary that has some zero values. What I want is a method of removing items from the dictionary when the they are equal to zero.

I found this previous thread here; seems like this used to work on a previous version, but I do not understand the workaround.

Community
  • 1
  • 1
Jack Walker
  • 238
  • 1
  • 2
  • 10
  • What exactly is the problem you are having? You want to iterate over the dictionary, removing keys where the value is equal to zero; where is your attempt to implement that, and what is going wrong with it? – jonrsharpe Jan 19 '15 at 11:20

2 Answers2

2

A dictionary can have zero values actually, if you want to remove them, just do it in your algorithm.

teams_goals = {'chelsea' : 2, 'man u' : 3,'Arsenal':1,'Man city':2}
print teams_goals
test_teams = {'chelsea' : 1, 'man u' : 1,'Arsenal':1}
teams_goals_copy = teams_goals.copy()

for team in test_teams:
    for j in range(test_teams[team]):
        teams_goals_copy[team]= teams_goals_copy[team]- 1
        if teams_goals_copy[team] == 0:
            del(teams_goals_copy[team])
    print teams_goals_copy
MiguelSR
  • 166
  • 1
  • 8
0

What you need is called comprehension. It exists for sets, lists, and dictionaries.

As you see here, you can iterate on members of your set, and choose only the ones you need. The output of the command is the ones selected, and you can use it as your copy.

Copy/pasting from that page, you have:

>>> print {i : chr(65+i) for i in range(4)}
{0 : 'A', 1 : 'B', 2 : 'C', 3 : 'D'}

>>> print {k : v for k, v in someDict.iteritems()} == someDict.copy()
1

>>> print {x.lower() : 1 for x in list_of_email_addrs}
{'barry@zope.com'   : 1, 'barry@python.org' : 1, 'guido@python.org' : 1}

>>> def invert(d):
...     return {v : k for k, v in d.iteritems()}
...
>>> d = {0 : 'A', 1 : 'B', 2 : 'C', 3 : 'D'}
>>> print invert(d)
{'A' : 0, 'B' : 1, 'C' : 2, 'D' : 3}

>>> {(k, v): k+v for k in range(4) for v in range(4)}
... {(3, 3): 6, (3, 2): 5, (3, 1): 4, (0, 1): 1, (2, 1): 3,
     (0, 2): 2, (3, 0): 3, (0, 3): 3, (1, 1): 2, (1, 0): 1,
     (0, 0): 0, (1, 2): 3, (2, 0): 2, (1, 3): 4, (2, 2): 4, (
     2, 3): 5}

You can read about other comprehensions (list and set) here.

Now in your case, here's a minumal example:

>>> my_dict = {'chelsea' : 2, 'man u' : 3,'Arsenal':1,'Man city':0}
>>> print(my_dict)
{'man u': 3, 'Man city': 0, 'Arsenal': 1, 'chelsea': 2}
>>> second_dict = {x:y for x,y in my_dict.items() if y > 0}
>>> print(second_dict)
{'man u': 3, 'Arsenal': 1, 'chelsea': 2}
adrin
  • 4,511
  • 3
  • 34
  • 50