Randomly Shuffle List Elements

  • Share this:

Code introduction


This function takes a list as input, then uses the sample function from the random module to randomly shuffle the elements in the list and returns the shuffled list. If the input is not a list, it raises a ValueError.


Technology Stack : random, list

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random

def shuffle_list(input_list):
    """
    Shuffle the elements in the input list and return the shuffled list.
    """
    if not isinstance(input_list, list):
        raise ValueError("Input must be a list.")

    return random.sample(input_list, len(input_list))                
              
Tags: