AndroidDev
AndroidDev

Reputation: 21237

Python: Using heap commands on a list of tuples

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!

Upvotes: 1

Views: 8851

Answers (2)

Lauritz V. Thaulow
Lauritz V. Thaulow

Reputation: 50995

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

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

Upvotes: 9

LukeJenx
LukeJenx

Reputation: 63

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

Upvotes: 2

Related Questions