Is there better ways to randomly shuffle two related lists without breaking their correspondence in the other list? I\'ve found related questions in numpy.array
Given the relationship demonstrated in the question, I'm going to assume the lists are the same length and that list1[i]
corresponds to list2[i]
for any index i
. With that assumption in place, shuffling the lists is as simple as shuffling the indices:
from random import shuffle
# Given list1 and list2
list1_shuf = []
list2_shuf = []
index_shuf = list(range(len(list1)))
shuffle(index_shuf)
for i in index_shuf:
list1_shuf.append(list1[i])
list2_shuf.append(list2[i])