{"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> 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 Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to shuffle elements of a Given Tuple.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" 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 …<\/p>\n
\nTuple in Python:<\/strong><\/p>\nGiven Tuple = (74, 65, 8, 100, 67, 122, 132, 56, 13, 89)<\/pre>\n
The given tuple after Shuffling the elements = (56, 122, 65, 13, 100, 74, 132, 89, 8, 67)<\/pre>\n
Given Tuple = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9,)<\/pre>\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
\n
Method #1: Using random.shuffle() Method (Static input)<\/h3>\n
\n
# Import the random module using import method.\r\nimport random\r\n# Give the tuple as static input and store it in a variable.\r\ngvn_tup = (6, 7, 8, 2, 45, 12, 63, 89)\r\n# Convert the given tuple into list using the list() function and\r\n# store it in another variable.\r\nlst = list(gvn_tup)\r\n# Apply random.shuffle() method on the above obtained list .\r\nrandom.shuffle(lst)\r\n# Convert the above shuffled list into tuple again using the tuple() function\r\n# and store it in another variable.\r\nshuffld_tupl = tuple(lst)\r\n# Print the above given tuple after shuffling the elements of a given tuple.\r\nprint(\"The given tuple after Shuffling the elements = \", shuffld_tupl)\r\n<\/pre>\n
The given tuple after Shuffling the elements = (45, 12, 6, 89, 63, 8, 2, 7)<\/pre>\n
Method #2: Using random.shuffle() Method (User input)<\/h3>\n
\n
# Import the random module using import method.\r\nimport random\r\n# Give the tuple as user input using tuple(),map(),input(),and split() functions\r\n#and Store it in a variable.\r\ngvn_tup = tuple(map(int, input(\r\n 'Enter some random tuple Elements separated by spaces = ').split()))\r\n# Convert the given tuple into list using the list() function and\r\n# store it in another variable.\r\nlst = list(gvn_tup)\r\n# Apply random.shuffle() method on the above obtained list .\r\nrandom.shuffle(lst)\r\n# Convert the above shuffled list into tuple again using the tuple() function\r\n# and store it in another variable.\r\nshuffld_tupl = tuple(lst)\r\n# Print the above given tuple after shuffling the elements of a given tuple.\r\nprint(\"The given tuple after Shuffling the elements = \", shuffld_tupl)\r\n<\/pre>\n
Enter some random tuple Elements separated by spaces = 0 1 2 3 4 5 6 7 8 9\r\nThe given tuple after Shuffling the elements = (8, 0, 5, 1, 6, 3, 7, 9, 4, 2)<\/pre>\n
\n