Božo Stojković
Božo Stojković

Reputation: 2943

Iterating through dictionary value of type list gets me TypeError: 'int' object is not iterable

I made a dictionary that has ints as indexes and lists as values. Upon trying to iterate through the value of type list, it gets me this error:

Traceback (most recent call last):
  File "C:\Python27\PyScripts\TextAdventure\InputModule.py", line 112, in <module>
    Inputs.update()
  File "C:\Python27\PyScripts\TextAdventure\InputModule.py", line 85, in update
    for priority, conditionals, function, args, kwargs in self.event_hooks[event.type]:
TypeError: 'int' object is not iterable

This is the class with relevant functions:

class InputManager(object):

    def hook(self, event_type, function, priority = -1, conditionals = [], *args, **kwargs):
        if not event_type in self.tracked_events:
            self.tracked_events.append(event_type)
        self.event_hooks[event_type] = [priority, conditionals, function, args, kwargs]

    def update(self):
        events = self.event_loop()

        to_call = {}
        for event in events:

            ## random test
            print "Dictionary:\n", self.event_hooks
            print "Index of dictionary:", event.type
            print "Value:\n", self.event_hooks[event.type]
            print "Value type:", type(self.event_hooks[event.type])
            print "Generated list:\n", [i for i in self.event_hooks[event.type]]
            print "Value list equality:", self.event_hooks[event.type] == [i for i in self.event_hooks[event.type]]
            ##

            for priority, conditionals, function, args, kwargs in self.event_hooks[event.type]:

                if conditionals and self.check_conditionals(event, conditionals):
                    to_call[priority] = to_call.get(priority, [])
                    to_call[priority].append([function, args, kwargs])

Inputs = InputManager()
Inputs.hook(pygame.MOUSEBUTTONUP, Inputs.terminate)
Inputs.update()

Now, as you can see, I've already put some random tests to see what is going on.

From the results, all I can conclude is that the object I am trying to iterate through is definitely not an int, but a list. Here they are:

Dictionary:
{6: [-1, [], <bound method InputManager.terminate of <__main__.InputManager object at 0x02A7BEB0>>, (), {}]}
Index of dictionary: 6
Value:
[-1, [], <bound method InputManager.terminate of <__main__.InputManager object at 0x02A7BEB0>>, (), {}]
Value type: <type 'list'>
Generated list:
[-1, [], <bound method InputManager.terminate of <__main__.InputManager object at 0x02A7BEB0>>, (), {}]
Value list equality: True

What is interesting to me is that all of those tests prove that I am not trying to iterate through a list. Or am I doing something wrong?

Upvotes: 1

Views: 459

Answers (2)

camz
camz

Reputation: 605

You are essentially doing something like

for a, b, c, d, e in [1, 2, 3, 4, 5]:
    ...

On the first pas through the loop, you get a value of 1 which you then try to assign to a,b,c,d,e

you are looping through each item in the list and then trying to unpack that into several variables. The first item in the list is an integer ([-1, [], <bound method ...) which you try and unpack into priority, conditionals, function, args, kwargs. You can't iterate through the integer though.

It looks like you probably just want to do this:

priority, conditionals, function, args, kwargs = self.event_hooks[event.type]

Upvotes: 3

konart
konart

Reputation: 1834

What you are trying to do here:

for priority, conditionals, function, args, kwargs in self.event_hooks[event.type]:

is to take 5 elements out of the each element of the list at key event.type of dictionarty event_hooks

This would work if each element of that list was iterable (another list of exactly 5 elements for example).

Your very first element, however is an integer, so you can't unpack anything from it.

Upvotes: 2

Related Questions