1

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!

AndroidDev
  • 20,466
  • 42
  • 148
  • 239

2 Answers2

9

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

myList = [('a', 1), ('b', 2)]
heapify(myList)
Lauritz V. Thaulow
  • 49,139
  • 12
  • 73
  • 92
  • 1
    Thanks for the reply, lazyr. It's easy for someone new to the language to miss something like that. Something that Ber might want to keep in mind. – AndroidDev Oct 23 '12 at 20:25
2

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

LukeJenx
  • 63
  • 1
  • 10
  • Thanks. I am using this as a reference to learn about binary heaps, but I just overlooked the fact that my data structure was instantiated as a tuple when I meant to create it as a list. – AndroidDev Oct 23 '12 at 20:53