Why Python crashes error IndexError: list index out of range when correct code?

Is an elementary part of the code that throws in a variable a random element from the list. Such a plan:
...
text_list = ['text1', 'text2', 'text3', 'text4']
get_text = random.choice(text_list)
...
Further work with this variable. List text_list - never changes. Index of references no. And it is on the string get_text =.. often crashes error IndexError: list index out of range. Don't know how this is possible? Most of the time the code works flawlessly, but sometimes it's like Python mind begins to go at all random.choice starts to fly index errore (lists are immutable and local!). The only time code operates in megapolice, flows more than 1K. But the variable is local to each object. Can anyone suggest what is this and how to avoid it? Don't wrap in a try except with a primitive that does not have lag? Maybe the problem is just a bug that crashes when many threads simultaneously try to access the shared item list?
June 8th 19 at 16:51
1 answer
June 8th 19 at 16:53
random.choice throws an exception if the sequence is empty.

the source is random.choice

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

Here is the address on the index and for generating the random number takes the length of the sequence. See why the list is empty or unavailable, if it is a shared resource.

Find more questions by tags Python