Nesa
Nesa

Reputation: 2965

Iterate a list through a dictionary

I have a list with the same values as the keys of a dictionary. I want to write a code that does something to the values of the dictionary (e.g. increases them by one) as many times as their key appears in the list.

So e.g.

listy=['dgdg','thth','zuh','zuh','thth','dgdg']
dicty = {'dgdg':1, 'thth':2, 'zuh':5}

I tried this code:

def functy (listx,dictx):
    for i in range (0, len(listx)):
        for k,v in dictx:
            if listx[i]==k:
                v=v+1
            else:
                pass
functy(listy, dicty)

But it raises this error:

Traceback (most recent call last):
  File "C:\Python34\8.py", line 12, in <module>
    functy(listy, dicty)
  File "C:\Python34\8.py", line 6, in functy
    for k,v in dictx:
ValueError: too many values to unpack (expected 2)

Could you tell me why it doesn't work and how I can make it?

Upvotes: 0

Views: 93

Answers (7)

Steven Rumbalski
Steven Rumbalski

Reputation: 45562

I suggest you use collections.Counter, which is a dict subclass for counting hashable objects.

>>> import collections
>>> count_y = collections.Counter(dicty) # convert dicty into a Counter
>>> count_y.update(item for item in listy if item in count_y)
>>> count_y 
Counter({'zuh': 7, 'thth': 4, 'dgdg': 3})

Upvotes: 2

Sebastian Wozny
Sebastian Wozny

Reputation: 17526

dict.__iter__ will by default refer to dict.keys().

Because you want both the key and its value it should be

for k,v in dictx.items():

which will yield a list of tuples:

>>> a={1:2,2:3,3:4}
>>> a.items()
[(1, 2), (2, 3), (3, 4)]

iteritems is also available, but yields from a generator instead of a list:

>>> a.iteritems()
<dictionary-itemiterator object at 0x00000000030115E8>

However, you should take into consideration directly indexing by key, otherwise your assignment v=v+1 will not be persisted to the dict:

def functy (listx,dictx):
    for item in listx:
        if item in dictx:
            dictx[item]+=1

>>> listy=['dgdg','thth','zuh','zuh','thth','dgdg']
>>> dicty = {'dgdg':1, 'thth':2, 'zuh':5}            
>>> print dicty
{'thth': 2, 'zuh': 5, 'dgdg': 1}
>>> functy(listy, dicty)
>>> print dicty
{'thth': 4, 'zuh': 7, 'dgdg': 3}

Upvotes: 5

LetzerWille
LetzerWille

Reputation: 5668

listy=['dgdg','thth','zuh','zuh','thth','dgdg']
dicty = {'dgdg':1, 'thth':2, 'zuh':5}

# items() missed and also dicty not updated in the original script
def functy (listx,dictx):
    for i in range (0, len(listx)):
        for k,v in dictx.items():
            if listx[i]==k:
                dictx[k] += 1
            else:
                pass
functy(listy, dicty)

print(dicty)

{'dgdg': 3, 'thth': 4, 'zuh': 7}

Upvotes: -1

nneonneo
nneonneo

Reputation: 179717

It looks like you're trying to use a dictionary as a counter. If that's the case, why not use the built-in Python Counter?

from collections import Counter
dicty = Counter({'dgdg':1, 'thth':2, 'zuh':5})
dicty += Counter(['dgdg','thth','zuh','zuh','thth','dgdg'])

# dicty is now Counter({'zuh': 7, 'thth': 4, 'dgdg': 3})

Upvotes: 4

mic4ael
mic4ael

Reputation: 8320

dictx.items() instead of dictx. When trying to iterate over dictx you are receiving only keys.

Upvotes: 0

tzaman
tzaman

Reputation: 47860

You're missing the point of having a dictionary, which is that you can index it directly by key instead of iterating over it:

def functy(listx, dictx):
    for item in listx:
        if item in dictx:
            dictx[item] += 1

Upvotes: 4

stenci
stenci

Reputation: 8501

You can iterate a dictionary like this:

for k in dictx:
    v = dictx[k]

Upvotes: 0

Related Questions