Take the 2-minute tour ×

I have

l = heapify([5,4,9,1])

and if I do a


It says it's NoneType instead of list type, anyone know what I'm doing wrong??

share|improve this question
It's generally considered bad style to use l as a variable name because it can so easily be mistaken for 1 or I depending on the font. –  agf Sep 19 '11 at 2:30
add comment

2 Answers

up vote 3 down vote accepted

The heapify() method transforms the list in-place. This means that it alters the list, but does not returned the modified list. As agf mentions below, heapify() returns None to protect you from this mistake. Therefore, if you do

lst = [5,4,9,1]

you will see that lst is now heapified. See the library reference for more info.

share|improve this answer
It's worth adding that all (or nearly all) of the Python methods that act in place return None, so you don't accidentally use an in-place method when you didn't mean to. –  agf Sep 19 '11 at 2:26
Alex Martelli himself explains it well here: stackoverflow.com/questions/1682567/… –  steveha Sep 19 '11 at 2:58
You will get a None from every function that doesn't explicitly return a value. def f(): pass for instance. –  etuardu Oct 9 '11 at 17:30
add comment

heapify mutates the list passed to it; just like how l.sort() does.

>>> import heapq
>>> l = [9, 8, 7, 6]
>>> heapq.heapify(l)
>>> l
[6, 8, 7, 9]
share|improve this answer
add comment

Your Answer


