Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

heapify returns NoneType

Tags:

python

I have

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

and if I do a

type(b)

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

like image 914
bb2 Avatar asked Sep 19 '11 02:09

bb2


2 Answers

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]
heapify(lst)
type(lst)

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

like image 60
brc Avatar answered Oct 01 '22 13:10

brc


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]
like image 28
Dan D. Avatar answered Oct 01 '22 12:10

Dan D.