Open In App

Python | Shuffle two lists with same order

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python list, we can have a problem in which we need to perform shuffle operation in list. This task is easy and there are straightforward functionalities available in Python to perform this. But sometimes, we need to shuffle two lists so that their shuffle orders are consistent. Let’s discuss a way in which this task can be performed.
Method : Using zip() + shuffle() + * operator 
In this method, this task is performed in three steps. Firstly, the lists are zipped together using zip(). Next step is to perform shuffle using inbuilt shuffle() and last step is to unzip the lists to separate lists using * operator. 
 

Python3




# Python3 code to demonstrate working of
# Shuffle two lists with same order
# Using zip() + * operator + shuffle()
import random
 
# initializing lists
test_list1 = [6, 4, 8, 9, 10]
test_list2 = [1, 2, 3, 4, 5]
 
# printing lists
print(f"The original list 1 : {test_list1}")
print(f"The original list 2 : {test_list2}")
 
# Shuffle two lists with same order
# Using zip() + * operator + shuffle()
temp = list(zip(test_list1, test_list2))
random.shuffle(temp)
res1, res2 = zip(*temp)
# res1 and res2 come out as tuples, and so must be converted to lists.
res1, res2 = list(res1), list(res2)
 
# Printing result
print(f"List 1 after shuffle :  {res1}")
print(f"List 2 after shuffle :  {res2}")


Output : 

 
The original list 1 : [6, 4, 8, 9, 10]
The original list 2 : [1, 2, 3, 4, 5]
List 1 after shuffle : [6, 10, 4, 8, 9]
List 2 after shuffle : [1, 5, 2, 3, 4]

 

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. zip() + shuffle() + * operator performs n*n number of operations.
Auxiliary Space: O(n*n), extra space is required where n is the number of elements in the list

Method #2: using random + zip()

Approach

We are using the zip() function to combine the two lists into a list of tuples, which is then shuffled using random.shuffle(). Finally, we are using zip() again to separate the shuffled tuples into two separate lists.

Algorithm

1. Create two original lists: list1 and list2
2. Combine the two lists using the zip() function and store the result in a variable called zipped
3. Shuffle the zipped list using the random.shuffle() function
4. Unzip the shuffled list back into two separate lists using zip() and store the result in list1 and list2 variables
5. Print the shuffled lists

Python3




import random
 
# original lists
list1 = [6, 4, 8, 9, 10]
list2 = [1, 2, 3, 4, 5]
 
# shuffle the lists with same order
zipped = list(zip(list1, list2))
random.shuffle(zipped)
list1, list2 = zip(*zipped)
 
# print shuffled lists
print("List 1 after shuffle:", list1)
print("List 2 after shuffle:", list2)


Output

List 1 after shuffle: (4, 9, 10, 6, 8)
List 2 after shuffle: (2, 4, 5, 1, 3)

Time complexity: O(NlogN) where N is the length of the input lists. This is because the zip() function takes O(N) time, random.shuffle() takes O(N) time, and zip() again takes O(N) time. The dominant factor is the random.shuffle() function which has a time complexity of O(NlogN).

Auxiliary Space: O(N) as we are creating a new list of tuples using zip(), which has a length of N.



Last Updated : 12 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads