Random Pairing of Lists

  • Share this:

Code introduction


This function takes two lists of equal length as input, then randomly pairs the elements of the two lists, and returns a list containing these pairs.


Technology Stack : random

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random

def shuffle_list(arg1, arg2):
    if not isinstance(arg1, list) or not isinstance(arg2, list):
        raise ValueError("Both arguments must be lists.")
    if len(arg1) != len(arg2):
        raise ValueError("Both lists must have the same length.")
    
    shuffled = [None] * len(arg1)
    random.shuffle(arg1)
    random.shuffle(arg2)
    
    for i in range(len(arg1)):
        shuffled[i] = (arg1[i], arg2[i])
    
    return shuffled                
              
Tags: