Shuffle in PythonShuffling refers to the rearranging of elements in a random order which means no specific sequence is considered while arranging the elements. In this tutorial, we will learn how we can shuffle the elements of a list using Python. The different approaches that we will use to shuffle the elements are as follows-
We will discuss each method in detail. So, let's begin with the first one, Using Fisher-Yates Shuffle AlgorithmOutput: The initialized list is : [11, 20, 19, 43, 22, 10] The shuffled list is : [11, 43, 20, 19, 10, 22] Explanation Let's understand what we have done in the above program.
Using shuffle()In the second method, we will see how shuffle() can be used to shuffle the elements of our list. Consider the program given below- Output: The initialized list is : [11, 20, 19, 43, 22, 10] The shuffled list is : [22, 10, 20, 11, 19, 43] Explanation Let's understand what we have done in the above program,
Using random.sample()In the third approach, we will use random.sample() to do the same. The following program illustrates how it can be done- Output: The initialized list is : [11, 20, 19, 43, 22, 10] The shuffled list is : [43, 20, 19, 11, 10, 22] Explanation It's time to understand the above program-
Finally, it's time to discuss the last approach, it's indeed an interesting one, let's see how…. Random Selection of Elements and then Appending them in a listOutput: The initialized list is : [11, 20, 19, 43, 22, 10] The shuffled list is : [19, 22, 20, 43, 10, 11] Explanation Now, let's see the explanation of this code,
ConclusionIn this tutorial, we learned the different methods of shuffling the elements of a list in Python. Next Topicfloor() and ceil() Functions in Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India