Randomly Shuffle List Elements

  • Share this:

Code introduction


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


Technology Stack : random

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random

def shuffle_list(input_list):
    """
    随机打乱列表中的元素顺序。
    """
    if not isinstance(input_list, list):
        raise TypeError("输入必须是列表类型")
    
    output_list = input_list[:]
    random.shuffle(output_list)
    return output_list                
              
Tags: