{"id":16822,"date":"2021-08-17T08:48:14","date_gmt":"2021-08-17T03:18:14","guid":{"rendered":"https:\/\/python-programs.com\/?p=16822"},"modified":"2021-11-22T18:37:23","modified_gmt":"2021-11-22T13:07:23","slug":"python-program-to-shuffle-elements-of-a-tuple","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-shuffle-elements-of-a-tuple\/","title":{"rendered":"Python Program to Shuffle Elements of a Tuple"},"content":{"rendered":"

In the previous article, we have discussed Python Program to Remove Duplicate elements from a Tuple<\/a>
\nTuple in Python:<\/strong><\/p>\n

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.<\/p>\n

shuffle method in python :<\/strong><\/p>\n

A tuple is an immutable type in Python. As a result, it cannot be shuffled directly.<\/p>\n

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.<\/p>\n

We can shuffle a list in Python by using an inbuilt method called shuffle from the random module.<\/p>\n

Examples:<\/strong><\/p>\n

Example 1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given Tuple = (74, 65, 8, 100, 67, 122, 132, 56, 13, 89)<\/pre>\n

Output:<\/strong><\/p>\n

The given tuple after Shuffling the elements =  (56, 122, 65, 13, 100, 74, 132, 89, 8, 67)<\/pre>\n

Example 2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given Tuple = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9,)<\/pre>\n

Output:<\/strong><\/p>\n

The given tuple after Shuffling the elements =  (5, 8, 6, 1, 3, 2, 9, 7, 0, 4)<\/pre>\n

Program to Shuffle Elements of a Tuple<\/h2>\n

Below are the ways to shuffle elements of a Given Tuple.<\/p>\n