Randomly Shuffle a List in Python

  • Share this:

Code introduction


This function takes a list as an argument and returns a new list that is a randomly shuffled version of the original list.


Technology Stack : random.sample

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random

def shuffle_list(items):
    if not isinstance(items, list):
        raise TypeError("items must be a list")
    
    shuffled = random.sample(items, len(items))
    return shuffled