Question

I'm trying to understand some of Python's built in heap functionality. It seems to not like things when I pass in a list of tuples (or more likely, I'm not passing the list in correctly). Here is what I have:

myList = ( ('a', 1), ('b', 2) )
heapify(myList)

The error that I get is

TypeError: heap argument must be a list

Am I doing something wrong? Is there another way to pass in a list of tuples?

Thanks!

Was it helpful?

Solution

The problem is that myList is a tuple. Try this:

myList = [('a', 1), ('b', 2)]
heapify(myList)

OTHER TIPS

As above heapify transforms a list (myList) into a heap. So if you want to use heapify you must translate everything to a list first.

http://docs.python.org/library/heapq.html <--- Gives you a bit more detail about heapq

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top