Answers for "shuffle python avec 2 lists the same way"

2

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
Posted by: Guest on November-11-2020

Code answers related to "shuffle python avec 2 lists the same way"

Python Answers by Framework

Browse Popular Code Answers by Language