Randomize List Order in Python (Challenge)

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Python, a list is an ordered collection of items. Sometimes, we may want to randomize the order of the items in a list. This can be useful for games, simulations, or any other situation where we want to introduce an element of randomness. In this challenge, you will write a function that shuffles the items in a list.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/ControlFlowGroup -.-> python/while_loops("`While Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13181{{"`Randomize List Order in Python (Challenge)`"}} python/while_loops -.-> lab-13181{{"`Randomize List Order in Python (Challenge)`"}} python/lists -.-> lab-13181{{"`Randomize List Order in Python (Challenge)`"}} python/tuples -.-> lab-13181{{"`Randomize List Order in Python (Challenge)`"}} python/function_definition -.-> lab-13181{{"`Randomize List Order in Python (Challenge)`"}} python/importing_modules -.-> lab-13181{{"`Randomize List Order in Python (Challenge)`"}} python/using_packages -.-> lab-13181{{"`Randomize List Order in Python (Challenge)`"}} python/standard_libraries -.-> lab-13181{{"`Randomize List Order in Python (Challenge)`"}} python/math_random -.-> lab-13181{{"`Randomize List Order in Python (Challenge)`"}} python/build_in_functions -.-> lab-13181{{"`Randomize List Order in Python (Challenge)`"}} end

Shuffle List

Problem

Write a function shuffle(lst) that takes a list as input and returns a new list with the same items in a randomized order. Your function should use the Fisher-Yates algorithm to shuffle the items in the list.

The Fisher-Yates algorithm works as follows:

  1. Start with the last item in the list.
  2. Generate a random index between 0 and the current index.
  3. Swap the item at the current index with the item at the random index.
  4. Move to the next item in the list and repeat steps 2-3 until all items have been shuffled.

Your function should not modify the original list. Instead, it should create a new list with the shuffled items.

You can assume that the input list will contain at least one item.

Example

foo = [1, 2, 3, 4, 5]
shuffled_foo = shuffle(foo)
print(shuffled_foo) ## [3, 1, 4, 5, 2]
print(foo) ## [1, 2, 3, 4, 5]

Summary

In this challenge, you wrote a function that shuffles the items in a list using the Fisher-Yates algorithm. This algorithm is a simple and efficient way to randomize the order of items in a list. By completing this challenge, you have gained a deeper understanding of how lists work in Python and how to manipulate them using functions.

Other Python Tutorials you may like