The concept of shuffle in Python comes from shuffling deck of cards. Shuffling is a procedure used to randomize a deck of playing cards to provide an element of chance in card games. Shuffling is often followed by a cut, to help ensure that the shuffler has not manipulated the outcome. In Python, the shuffle list is used to get a completely randomized python list. Show
The shuffle() function can be used to shuffle a list. The shuffle is performed in place, meaning that the list provided as an argument to the shuffle() function is shuffled rather than a shuffled copy of the list being made and returned. Contents Python Shuffle a List Using shuffle() FunctionThe shuffle() function in Python random module can be used to shuffle a list. Shuffling is performed in place, meaning that the list provided as an argument to the shuffle() function is shuffled rather than a shuffled copy of the list being made and returned. The shuffle() method takes a sequence (list, string, or tuple) and reorganizes the order of the items. Syntax of shuffle() Function to Python Shuffle Listrandom.shuffle(lst, function) Parameters
Return Value of shuffle() FunctionThis method returns the reshuffled list. Flowchart of shuffle() Function to Python Shuffle ListLet’s Move into the Examples. Example 1: Basic Python Program to Python Shuffle List Using shuffle() Function
Output:
Explanation: In the above example, we have shuffled the Python list of countries. First, we have imported the random module. As the shuffle method or function is present in the random Module. So we have to first import the random Module. After that, we have initialized and created our list of countries. Here you can create any Python list of your choice. Then we shuffled the Python List of countries using the shuffle() function. Example 2: Shuffling a List n n Number of Times Using for Loop and RangeUsing for loop we can shuffle the list for any number of times we want.
Output:
Explanation: In this second example, we have taken the Python list [’26’,’Python’,’Pool’,’is’,’Best’,’Way’,’To’,’Learn’,’Python’]. And we want to shuffle this list 10 times. So to get our desired result. We used the Python for loop with range 10 and initialized it. And inside that for loop, we are using the shuffle method of the random module. It will do the shuffling of the list as long as the loop goes on. Note: If you want to shuffle the list 20 times. You have to just change the parameter of range(20) function to 20. Example 3: Shuffling two Python List at once with the same orderLet’s assume if you want to Shuffle two Python lists. But also wants to maintain the same shuffle order. For example, One list contains Country name and the other contains a Capitals.
Output:
Example 1: Basic Python Program to Python shuffle list of listsAnother really interesting use case is when you have nested structures with similar data. In this case, you can simply iterate the lists and apply the shuffle function.
Output:
How to Shuffle a Deck of Card in PythonIn this section of the article, you’ll learn to shuffle a deck of cards in Python. Using the shuffle() Function random module. Python Code to Shuffle Cards
Output:
Explanation: Note: Run the program again to shuffle the cards. In the program, we used the 1 function in 2 module to create a deck of cards. This function performs the Cartesian product of the two sequences.The two sequences are numbers from 1 to 13 and the four suits. So, altogether we have 13 * 4 = 52 items in the deck with each card as a tuple. For example, 0Our deck is ordered, so we shuffle it using the function 3 in 4 module.Finally, we draw the first five cards and display it to the user. We will get different output each time you run this program as shown in our two outputs. |