Shuffle python list – Python Program to Shuffle a List

Shuffle python list: In the previous article, we have discussed Python Program to Print a Deck of Cards in Python
To shuffle the elements in a list means to arrange them in random order.

In Python, there are numerous ways to shuffle the elements of a list.

Like using random.shuffle() method , for loop etc.

Random Module in python :

Shuffle python list: As this Random module is one of Python’s predefined modules, its methods return random values.

It selects integers uniformly from a range. For sequences, it has a function to generate a random permutation of a list in-place, as well as a function to generate a random sampling without replacement. Let’s take a look at how to import the Random Module.

The random module in Python is made up of various built-in Methods.

The random module is extremely useful for creating a variety of entertaining games.

random.shuffle(): To shuffle an object, use random. shuffle().

Given a list and the task is to shuffle the elements of a given list.

Examples:

Example1:

Input:

Given list = ['good', 'morning', 'btechgeeks', '651326', '12345', 'great', 'coding', 'platform']

Output:

The given list after shuffling the elements = ['good', '651326', 'coding', 'platform', 'morning', 'great', 'btechgeeks', '12345']

Example2:

Input:

Given list =['potato' ,'tomato', 'carrot', 'brinjal, 'beetroot']

Output:

The given list after shuffling the elements = ['tomato', 'potato', 'brinjal', 'carrot', 'beetroot']

Program to Shuffle a List

Below are the ways to shuffle a given list.

Method #1: Using random.shuffle() Method (Static input)

Approach:

  • Import random module using the import keyword.
  • Give the list as static input and store it in another variable.
  • Apply random. shuffle() method for the above-given list to shuffle the items of a given list.
  • Print the shuffled list of the given input list.
  • The Exit of the program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the list as static input and store it in another variable.
gvn_lst = ['hello', 'this', 'is', 'btechgeeks', '12345']
# Apply random. shuffle() method for the above-given list to shuffle the items
# of a given list.
random.shuffle(gvn_lst)
# Print the shuffled list of the given input list.
print("The given list after shuffling the elements =", gvn_lst)

Output:

The given list after shuffling the elements = ['this', 'hello', '12345', 'is', 'btechgeeks']

Method #2: Using random.shuffle() Method (User input)

Approach:

  • Import random module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions and store it in another variable. 
  • Apply random. shuffle() method for the above-given list to shuffle the items of a given list.
  • Print the shuffled list of the given input list.
  • The Exit of the program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the list as user input using list(),map(),input(),and split() functions 
#and store it in another variable.
gvn_lst = list(map(str, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Apply random. shuffle() method for the above-given list to shuffle the items
# of a given list.
random.shuffle(gvn_lst)
# Print the shuffled list of the given input list.
print("The given list after shuffling the elements =", gvn_lst)

Output:

Enter some random List Elements separated by spaces = potato tomato carrot brinjal beetroot
The given list after shuffling the elements = ['tomato', 'potato', 'brinjal', 'carrot', 'beetroot']

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.