Program to Shuffle Elements of a Tuple

Python Program to Shuffle Elements of a Tuple

In the previous article, we have discussed Python Program to Remove Duplicate elements from a Tuple
Tuple in Python:

A tuple is an immutable list of objects. That means the elements of a tuple cannot be modified or changed while the program is running.

shuffle method in python :

A tuple is an immutable type in Python. As a result, it cannot be shuffled directly.

We can typecast it to a list (which is mutable), shuffle it, and then typecast it back to tuple to provide the output as a tuple.

We can shuffle a list in Python by using an inbuilt method called shuffle from the random module.

Examples:

Example 1:

Input:

Given Tuple = (74, 65, 8, 100, 67, 122, 132, 56, 13, 89)

Output:

The given tuple after Shuffling the elements =  (56, 122, 65, 13, 100, 74, 132, 89, 8, 67)

Example 2:

Input:

Given Tuple = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9,)

Output:

The given tuple after Shuffling the elements =  (5, 8, 6, 1, 3, 2, 9, 7, 0, 4)

Program to Shuffle Elements of a Tuple

Below are the ways to shuffle elements of a Given Tuple.

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

Approach:

  • Import the random module using the import method.
  • Give the tuple as static input and store it in a variable.
  • Convert the given tuple into a list using the list() function and store it in another variable.
  • Apply random. shuffle() method on the above-obtained list.
  • Convert the above-shuffled list into a tuple again using the tuple() function and store it in another variable.
  • Print the above-given tuple after shuffling the elements of a given tuple.
  • The Exit of the program.

Below is the implementation:

# Import the random module using import method.
import random
# Give the tuple as static input and store it in a variable.
gvn_tup = (6, 7, 8, 2, 45, 12, 63, 89)
# Convert the given tuple into list using the list() function and
# store it in another variable.
lst = list(gvn_tup)
# Apply random.shuffle() method on the above obtained list .
random.shuffle(lst)
# Convert the above shuffled list into tuple again using the tuple() function
# and store it in another variable.
shuffld_tupl = tuple(lst)
# Print the above given tuple after shuffling the elements of a given tuple.
print("The given tuple after Shuffling the elements = ", shuffld_tupl)

Output:

The given tuple after Shuffling the elements =  (45, 12, 6, 89, 63, 8, 2, 7)

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

Approach:

  • Import the random module using the import method.
  • Give the tuple as user input using tuple(),map(),input(),and split() functions and Store it in a variable.
  • Convert the given tuple into a list using the list() function and store it in another variable.
  • Apply random. shuffle() method on the above-obtained list.
  • Convert the above-shuffled list into a tuple again using the tuple() function and store it in another variable.
  • Print the above-given tuple after shuffling the elements of a given tuple.
  • The Exit of the program.

Below is the implementation:

# Import the random module using import method.
import random
# Give the tuple as user input using tuple(),map(),input(),and split() functions
#and Store it in a variable.
gvn_tup = tuple(map(int, input(
   'Enter some random tuple Elements separated by spaces = ').split()))
# Convert the given tuple into list using the list() function and
# store it in another variable.
lst = list(gvn_tup)
# Apply random.shuffle() method on the above obtained list .
random.shuffle(lst)
# Convert the above shuffled list into tuple again using the tuple() function
# and store it in another variable.
shuffld_tupl = tuple(lst)
# Print the above given tuple after shuffling the elements of a given tuple.
print("The given tuple after Shuffling the elements = ", shuffld_tupl)

Output:

Enter some random tuple Elements separated by spaces = 0 1 2 3 4 5 6 7 8 9
The given tuple after Shuffling the elements = (8, 0, 5, 1, 6, 3, 7, 9, 4, 2)

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.