random.shuffle() function in Python
sample() is an inbuilt method of the
random module. It is used to shuffle a sequence (list). Shuffling means changing the position of the elements of the sequence. Here, the shuffling operation is inplace.
Syntax : random.shuffle(sequence, function)
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
sequence : can be a list
function : optional and by default is random(). It should return a value between 0 and 1.
Returns : nothing
Example 1 : Shuffling a list
Original list : ['A', 'B', 'C', 'D', 'E'] After the first shuffle : ['A', 'B', 'E', 'C', 'D'] After the second shuffle : ['C', 'E', 'B', 'D', 'A']
shuffle() method cannot be used to shuffle immutable datatypes like strings.
Original list : ['A', 'B', 'C', 'D', 'E'] After the first shuffle : ['A', 'D', 'B', 'E', 'C'] After the second shuffle : ['A', 'D', 'B', 'E', 'C']