Randomly Shuffle List Elements Function

  • Share this:

Code introduction


This function takes a list as input and returns a new list with the elements shuffled randomly.


Technology Stack : list, 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.copy()
    random.shuffle(shuffled_list)
    return shuffled_list                
              
Tags: