[Python-Dev] random.choice IndexError on empty list

Pete Shinners pete at shinners.org
Thu Oct 23 16:22:59 EDT 2003


(this should potentialy go on sourceforge's bug tracker? alas i have 
no account right now)

Another user and I were scratching our heads over why random.choice() 
was raising "IndexError: list index out of range". For awhile we were 
thinking random.random() must have been returning >= 1.0. It turns out 
an empty list was being passed.

I would suggest either a ValueError is raised, or a different 
exception message. perhaps more like the results of

     >>> [].pop()
     Traceback (most recent call last):
       File "<stdin>", line 1, in ?
     IndexError: pop from empty list

The patch is trivial, but I can provide it if there is an agreed-apon 
response.





More information about the Python-Dev mailing list