Python Shuffle List Program

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

Shuffling a sequence of numbers is an important and useful concept used in real-life applications. Python shuffle list refers to the process of rearranging items of a list in random order.

Python Shuffle List

Knowing how to shuffle a list and produce a pseudo-random result is extremely useful for data-related work. Shuffling a list of items from an API call, a list of names from a database, and a list of items in a CSV file are some of the many practical examples of a Python shuffle list. Thus, knowing more than one method to achieve this is always helpful.

Shuffling a list

Methods to Shuffle List in Python

Fisher-Yates Shuffle Algorithm

The Fisher-Yates Shuffle Algorithm is one of the famous algorithms used to shuffle a sequence of items. It considers the higher index value and swaps it with the current value. The process is repeated multiple times to reorder the entire list.

Time Complexity

The Fisher-Yates shuffle algorithm works in O(n) time complexity. Assuming function rand() generates a random number in O(1) time, the time taken to shuffle all items in an array is proportional to the total number of items.

Space Complexity

The Fisher-Yates shuffle algorithm has a space complexity of O(1) `as it does not require any extra space.

Code

Output

Using random.shuffle()

The shuffle() is one of the most popular methods of the random library used to change the order of the elements in the list and place them randomly. It is implemented by importing the module random and then calling a list inside the shuffle() function. Since shuffling happens in place, the original order of the list is lost.

Time Complexity

The random. shuffle() function works in O(n) time complexity where n is the number of items in the list.

Space Complexity

The random. shuffle() function has a space complexity of O(1) as it does not require any extra space.

Code

Output

Using random.sample()

The in-built random library in Python has a sample() function that is used to return a new sampled list without altering the original one. This is done by importing module random and then calling the sample() function that needs two arguments, the original sequence and the number of sampled elements desired in the output list.

Time Complexity

The random. sample() function works in θ(mlog(n)) time complexity where m is the number of choices and n is the number of nodes to choose from.

Space Complexity

The random.sample() function has a space complexity of θ(m), where m is the length of the output list returned by the function.

Code

Output

Shuffling a list by selecting an index randomly and appending that element at that index to the list

This method is used to select an index randomly and append that element at that index to the end of the list. After importing the random module, its randint() function is used to choose an index. The element at that index is then deleted from that location and added to the end. This process is repeated multiple times to get a shuffled list.

Time Complexity

The random.randint() function takes O(1) time. Since the entire list is traversed using a for loop, the overall time complexity for this approach will be O(n).

Space Complexity

This approach has a space complexity of O(1), as it does not require any extra space.

Code

Output

Which is The Best Method to Shuffle a List in Python?

The most suitable method for Python shuffle list varies from program to program, depending on its requirement. random.shuffle() is preferred for in-place shuffling, that is items' positions are altered in the original list. random.sample() is preferred when the original orientation of the list needs to be preserved. However, random.shuffle() is the most recommended method to shuffle a list as it does in-place shuffling. The random library in Python provides this inbuilt function to shuffle the list in place.

FAQs

Q. How to randomize the items of a list in place?

A. The shuffle() method in the random library randomizes the items of a list in place.

Q. What is the difference between the Python shuffle list methods .shuffle() and .sample()?

A. random.shuffle() shuffles the original list and does not return anything. random.sample() returns a new shuffled list based on the original list without altering it.

Q. What is the time complexity of the Fisher-Yates shuffle algorithm?

A. The Fisher-Yates shuffle algorithm works in O(n) time complexity, as the time taken is proportional to the total number of items being shuffled.

Similar Python Programs

Conclusion

  • Fisher-Yates shuffle algorithm helps to generate random permutations by shuffling the items in place. This Python shuffle list algorithm swaps the element at each iteration at random among the unvisited indices, including the element itself. It works in O(n) time complexity, that is time taken is proportional to the total number of items being shuffled.
  • The shuffle() method takes a sequence of items and rearranges the order of the items. This modifies the original list and does not return anything.
  • The sample() method in Python shuffle list takes a sequence of items and returns a new rearranged list of the desired length. It does not alter the original list.