Randomly Shuffle Elements in a List Function

  • Share this:

Code introduction


Defined a function to randomly shuffle the elements of an input list.


Technology Stack : random (random number generation)

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random

def shuffle_list(input_list):
    """
    Randomly shuffles the elements of the input list.

    Args:
        input_list (list): The list to be shuffled.

    Returns:
        list: A new list with elements shuffled.
    """
    shuffled_list = input_list.copy()
    random.shuffle(shuffled_list)
    return shuffled_list