{"id":25542,"date":"2021-11-23T08:56:38","date_gmt":"2021-11-23T03:26:38","guid":{"rendered":"https:\/\/python-programs.com\/?p=25542"},"modified":"2021-11-23T08:56:38","modified_gmt":"2021-11-23T03:26:38","slug":"python-random-shuffle-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-random-shuffle-method-with-examples\/","title":{"rendered":"Python Random shuffle() Method with Examples"},"content":{"rendered":"
Random shuffle() Method in Python:<\/strong><\/p>\n The shuffle() method reorganizes the order of items in a sequence, such as a list.<\/p>\n Note:<\/strong> Please keep in mind that this method modifies the original list and does not return a new list.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n sequence:<\/strong> This is Required. It could be any sequence.<\/p>\n function:<\/strong> This is Optional. The name of a function that produces a value between 0.0 and 1.0. Return Value:<\/strong> This method produces no output.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n To weigh or specify the result, you can define your own function.<\/p>\n If the function returns the same number every time, the result will be the same every time<\/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 <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Random shuffle() Method in Python: The shuffle() method reorganizes the order of items in a sequence, such as a list. Note: Please keep in mind that this method modifies the original list and does not return a new list. Syntax: random.shuffle(sequence, function) Parameters sequence: This is Required. It could be any sequence. function: This is …<\/p>\nrandom.shuffle(sequence, function)<\/pre>\n
\nIf no function is specified, the random() function will be used.<\/p>\nGiven List = [\"good\", \"morning\", \"Btechgeeks\"]<\/pre>\n
Given list After shuffling : ['Btechgeeks', 'morning', 'good']<\/pre>\n
Given List = [10, 20, 30, 40]<\/pre>\n
Given list After shuffling : [20, 40, 30, 10]<\/pre>\n
Random shuffle() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [\"good\", \"morning\", \"Btechgeeks\"]\r\n# Pass the given list to the random.shuffle() method that reorganizes the order of\r\n# items in a given list.\r\nrandom.shuffle(gvn_lst)\r\n# Print the above-given list after shuffling randomly.\r\nprint(\"Given list After shuffling :\", gvn_lst)\r\n<\/pre>\n
Given list After shuffling : ['Btechgeeks', 'morning', 'good']<\/pre>\n
Using Function<\/h5>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n\r\n# Create a function say demo_function.\r\n\r\n\r\ndef demo_function():\r\n # Inside the function, return 0.1\r\n return 0.1\r\n\r\n\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [\"hello\", \"this\", \"is\", \"btechgeeks\"]\r\n# Pass the given list, above function as arguments to the random.shuffle() method\r\n# in which if the function returns the same number every time, the result will be\r\n# the same every time.\r\n\r\nrandom.shuffle(gvn_lst, demo_function)\r\n# Print the above-given list after shuffling.\r\nprint(gvn_lst)\r\n<\/pre>\n
['this', 'is', 'btechgeeks', 'hello']<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n\r\n# Pass the given list to the random.shuffle() method that reorganizes the order of\r\n# items in a given list.\r\nrandom.shuffle(gvn_lst)\r\n# Print the above-given list after shuffling randomly.\r\nprint(\"Given list After shuffling :\", gvn_lst)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 10 20 30 40\r\nGiven list After shuffling : [20, 40, 30, 10]<\/pre>\n