This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: IndexError msg of random.choice() not helpful
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.9
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: mark.dickinson, rhettinger, wyz23x2
Priority: normal Keywords:

Created on 2021-02-02 04:07 by wyz23x2, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg386128 - (view) Author: wyz23x2 (wyz23x2) * Date: 2021-02-02 04:07
When you apply `random.choice` on empty sequences:
>>> import random
>>> random.choice([])
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
    return seq[self._randbelow(len(seq))]
IndexError: list index out of range
This message doesn't clearly state the real problem -- an empty seq.
Meanwhile, many other methods give messages.
>>> [].pop()
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
IndexError: pop from empty list
>>> import collections
>>> collections.deque().popleft()
Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
IndexError: pop from an empty deque
>>> random.randrange(0, 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Python39\lib\", line 316, in randrange
    raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (0, 0, 0)

P.S. Both are empty sequences/ranges, randrange() raises ValueError, while choice() raises IndexError.
msg386141 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-02-02 13:25
-1 on changing this because we would have to gum-up and slow down the code with an extra try-except to catch and reraise the exception with a different error message:

    def choice(self, seq):
        """Choose a random element from a non-empty sequence."""
            return seq[self._randbelow(len(seq))]
        except IndexError:
            raise IndexError('Cannot choose from an empty sequence') from None

We rarely do that elsewhere in the code.  The norm in pure python code is that indexing into an empty list shows "IndexError: list index out of range" without further elaboration on what it means for the list to be empty.

FWIW, this behavior is very old and doesn't seem to have been a problem in practice.  Here is what the code looked like in Python 2.1:

    def choice(self, seq):
        """Choose a random element from a non-empty sequence."""
        return seq[int(self.random() * len(seq))]
Date User Action Args
2022-04-11 14:59:40adminsetgithub: 87263
2021-02-05 03:48:07rhettingersetstatus: open -> closed
resolution: wont fix
stage: resolved
2021-02-02 13:25:33rhettingersetmessages: + msg386141
2021-02-02 12:57:14rhettingersetassignee: rhettinger
2021-02-02 04:28:32xtreaksetnosy: + rhettinger, mark.dickinson
2021-02-02 04:07:52wyz23x2create