About Lesson
shuffle() Method
The shuffle() method takes a sequence, like a list, and reorganize the order of the items.
Note: This method changes the original list, it does not return a new list.
Syntax
random.shuffle(sequence)
Parameter Values
Parameter |
Description |
sequence |
Required. A sequence. |
function |
Deprecated since Python 3.9. Removed in Python 3.11. |
Example
Shuffle a list (reorganize the order of the list items):
import random
mylist = [“apple”, “banana”, “cherry”]
random.shuffle(mylist)
print(mylist)
Output:
[“apple”, “cherry”, “banana”]