Shuffle List Function

  • Share this:

Code introduction


This function takes a list as input and returns a shuffled version of it. It uses random number generation to swap elements in the list.


Technology Stack : random

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random

def shuffle_list(input_list):
    """
    This function takes a list and returns a shuffled version of it.
    """
    shuffled_list = input_list.copy()
    for i in range(len(shuffled_list) - 1, 0, -1):
        j = random.randint(0, i)
        shuffled_list[i], shuffled_list[j] = shuffled_list[j], shuffled_list[i]
    return shuffled_list                
              
Tags: