Randomly Shuffle List Elements

  • Share this:

Code introduction


This function takes a list as an argument, then uses the shuffle method from the random library to randomly shuffle the elements in the list, and finally returns a new list with the elements shuffled.


Technology Stack : random

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random

def shuffle_list(items):
    """
    Randomly shuffle the elements of the list.

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

    Returns:
        list: A new list with the elements shuffled.
    """
    shuffled = items.copy()
    random.shuffle(shuffled)
    return shuffled                
              
Tags: