Write a program to make Shuffle list items in Python

In this tutorial we will learn how to write a program to make shuffle list items in python with some coding example. In many situations you might have to come up with this type of requirements.
I know you are here just because you are in need of this awesome trick to make a list shuffle means changing the position of the elements of the sequence in a list using Python.
If you don’t know how to use the random function to shuffle a list then you’re at the right place. Because in this tutorial we gonna find out how the random library provides this inbuilt function which in-place shuffles the list in Python. And this method choose to save time and hustle.

Shuffle list items in Python

Let’s learn this with a easy coding examples,
1. At First, import the random module, which provides various functions for generating random numbers and shuffling sequences.

import random

2. Then we take an empty list (original_list in this example) , Where we will store user inputs.

original_list=[]

3. After that, We Use the prompt that helps the user to enter an integer(r), Which determine how many values they want to input.

r=int(input("enter your range:"))

4. Then we starts a loop that runs r times. Under the loop we use a prompt that helps the user to enter a value(l), which is then append to the original list.

for i in range(r):
l= input("enter the value: ")
original_list.append(l)

5. After creating the original list, We Create another list called shuffled list, And put all the value of the original list into it.

shuffled_list = original_list.copy()

6. And now we use the main element of this code, that is we shuffle the list items that are present in the shuffled list using random.shuffle() function.

random.shuffle(shuffled_list)

7. Finally, print the Original list and shuffled list to see the result. And it’s also helps us to clearly see the difference between them.

print("Original list:", original_list)
print("Shuffled list:", shuffled_list)
The final Python code is :
# Step 1: At first import the 'random' module
import random
# Step 2: Initialize an empty list to store user inputs
original_list=[]
# Step 3: Take user input to determine the range of inputs
r=int(input("enter your range:"))
# Step 4: Loop to collect user inputs and append them to original_list
for i in range(r):
    l= input("enter the value: ")
    original_list.append(l)
# Step 5: Create a copy of the original list
shuffled_list = original_list.copy()
# Step 6: Shuffle the copied list using random.shuffle() function
random.shuffle(shuffled_list)
# Step 7: Print both the original and shuffled lists
print("Original list:", original_list)
print("Shuffled list:", shuffled_list)
Execution of the code:

If the user enters a range (r) of 3 and inputs “apple”, “banana”, “berry” respectively, the output could be:

Enter your range: 3
Enter the value: apple
Enter the value: banana
Enter the value: berry
Original list: ['apple', 'banana', 'berry']
Shuffled list: ['banana', 'berry', 'apple']

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top