Shuffling List Elements with Python

  • Share this:

Code introduction


This function takes a list as input and then shuffles the elements of the list in place using the shuffle method from the random module.


Technology Stack : random

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random

def shuffle_list(input_list):
    if not isinstance(input_list, list):
        raise ValueError("The input must be a list.")
    random.shuffle(input_list)
    return input_list                
              
Tags: