Randomly Shuffle a List

  • 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 : List operation

Code Difficulty : Beginner


                
                    
import random

def shuffle_list(input_list):
    """
    随机打乱列表中的元素顺序。
    """
    if not isinstance(input_list, list):
        raise ValueError("Input must be a list.")
    
    random.shuffle(input_list)
    return input_list                
              
Tags: