Randomly Shuffle List Function

  • Share this:

Code introduction


This function takes a list as input and then uses the shuffle method from the random library to randomly shuffle the elements of the list, and returns the shuffled list.


Technology Stack : List (list), Random (random)

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random

def shuffle_list(input_list):
    if not isinstance(input_list, list):
        raise TypeError("input_list must be a list")
    shuffled_list = input_list[:]
    random.shuffle(shuffled_list)
    return shuffled_list