I am performing shuffle operations on nested lists in Python 3. I wish to reshuffle previously shuffled lists indefinitely until the order of the nested lists meet a specific criteria. random.shuffle operates in place and calling random.shuffle() on a previously shuffled list does not reshuffle it. What is the best way to reshuffle a list indefinitely until it meets a condition. For example, I was trying something like this, but making a new list then shuffling it doesn’t seem to work:
from random import shuffle
L1 = [[1,2], [3,4], [5,6], [7,8], [9,10]]
shuffle(L1)
match = L1[0]
# reshuffle until [9,10] is the first item in the list
if match != [9,10]:
L1 = list(L1)
shuffle(L1)
print(L1)
This statement:
This is incorrect. Observe:
The following code should do what you want, although it will have a non-deterministic runtime.