Let's say I have a list: a = [1,1]
.
If I call a.index(1)
it will always return 0
.
Is there any pythonic way to return 0
or 1
in equal probabilities?
Let's say I have a list: a = [1,1]
.
If I call a.index(1)
it will always return 0
.
Is there any pythonic way to return 0
or 1
in equal probabilities?
As stated here:
The syntax of the index method is: list.index(x[, start[, end]])
.
It also says:
The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.
So a possible way could be the use of random as the start
argument:
import random
a = [1, 1, 1, 1, 1, 1]
index_method = a.index(1)
index_method_with_start = a.index(1, random.randint(0, len(a) -1))
print(index_method, index_method_with_start)