Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Randomising Lists in Python

Learn how to randomise a list in Python with step-by-step instructions, code snippets, and explanations. Understand the relationship between lists and Python’s built-in functions for generating permut …


Updated June 6, 2023

Learn how to randomise a list in Python with step-by-step instructions, code snippets, and explanations. Understand the relationship between lists and Python’s built-in functions for generating permutations.

Definition of Randomising a List

Randomising a list in Python refers to rearranging its elements in a seemingly random order. This can be achieved using various algorithms and techniques that generate different permutations of the original list. In this article, we will explore how to randomise a list in Python, focusing on the built-in random module.

Why Randomise Lists?

Randomising lists is useful in various scenarios:

  • Simulation: Generating randomised datasets for simulations or testing.
  • Game Development: Creating games that require shuffled decks of cards or randomised levels.
  • Data Analysis: Permuting data to explore different relationships or distributions.

Step-by-Step Guide: Randomising a List using the random Module

Method 1: Using the shuffle() Function

The random.shuffle() function is specifically designed for shuffling lists in-place. It uses a Fisher-Yates shuffle algorithm, which is an efficient and unbiased method for generating random permutations.

import random

my_list = [1, 2, 3, 4, 5]
print("Original list:", my_list)

random.shuffle(my_list)
print("Shuffled list:", my_list)

In this example, the shuffle() function takes the original list as input and shuffles its elements in-place. The resulting shuffled list is then printed to the console.

Method 2: Using the sample() Function

The random.sample() function generates a randomised subset of elements from the original list. It can be used when you need to select a specific number of unique elements from the list.

import random

my_list = [1, 2, 3, 4, 5]
print("Original list:", my_list)

random_sample = random.sample(my_list, len(my_list))
print("Random sample:", random_sample)

In this example, the sample() function takes two arguments: the original list and the desired length of the subset. It returns a new list containing the randomly selected elements.

Code Explanation

  • import random: This line imports the built-in random module, which provides various functions for generating random numbers and shuffling lists.
  • my_list = [1, 2, 3, 4, 5]: This line creates a sample list containing five elements.
  • random.shuffle(my_list): This line uses the shuffle() function to shuffle the elements of the original list in-place.
  • random.sample(my_list, len(my_list)): This line uses the sample() function to generate a randomised subset of elements from the original list.

Conclusion

Randomising lists in Python is an essential technique for various applications, including simulation, game development, and data analysis. In this article, we explored two methods for shuffling lists: using the shuffle() function and the sample() function. By following these step-by-step instructions and code snippets, you can generate randomised permutations of your lists in Python.

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp