In-Place List Shuffling Function

  • Share this:

Code introduction


The function shuffles the elements of the input list in place, meaning it does not create a new list but operates on the original list.


Technology Stack : random

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random

def shuffle_list(input_list):
    """
    Shuffle the elements of the input list in place.
    """
    for i in range(len(input_list) - 1, 0, -1):
        j = random.randint(0, i)
        input_list[i], input_list[j] = input_list[j], input_list[i]
    return input_list                
              
Tags: