how to shuffle two lists the same way python code example

Example 1: python shuffle two lists in the same way

# Example usage using random:
import random
# Say you want to shuffle (randomly reorder) the following lists in the
# same way (e.g. because there's an association between the elements that
# you want to maintain):
your_list_1 = ['the', 'original', 'order']
your_list_2 = [1, 2, 3]

# Steps to shuffle:
joined_lists = list(zip(your_list_1, your_list_2))
random.shuffle(joined_lists) # Shuffle "joined_lists" in place
your_list_1, your_list_2 = zip(*joined_lists) # Undo joining
print(your_list_1)
print(your_list_2)
--> ('the', 'order', 'original') # Both lists shuffled in the same way
--> (1, 3, 2) # Use list(your_list_2) to convert to list

Example 2: python shuffle two lists together

from sklearn.utils import shuffle
a_shuffled, b_shuffled = shuffle(np.array(a), np.array(b))