Shuffling a List of Numbers Using Random Module

Write a Python program that uses the random module to shuffle a list of numbers. Return the shuffled list.

Example 1:

Input: [1, 2, 3, 4, 5]
Output: [3, 1, 5, 4, 2]  # Output may vary

Example 2:

Input: [6, 7, 8, 9, 10]
Output: [9, 7, 10, 8, 6]  # Output may vary

Use the shuffle() function from the random module.

import random

def shuffle_list(num_list):
    random.shuffle(num_list)
    return num_list

# Test the function
print(shuffle_list([1, 2, 3, 4, 5]))  # Output: A shuffled version of the list
print(shuffle_list([6, 7, 8, 9, 10]))  # Output: A shuffled version of the list

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!