Randomly Shuffle a List in Python

  • Share this:

Code introduction


This function takes a list as input and returns a randomly shuffled version of the list. It uses the sample method from the random module to randomly select elements from the list while maintaining the same number of elements.


Technology Stack : random

Code Type : List operation

Code Difficulty : Intermediate


                
                    
import random

def shuffle_list(input_list):
    """
    将输入的列表进行随机排序。

    参数:
    input_list (list): 需要随机排序的列表。

    返回:
    list: 随机排序后的列表。
    """
    return random.sample(input_list, len(input_list))                
              
Tags: