This method shuffles the given string or an array. It randomizes the items in the array. This method is present in the random module. So, we need to import it and then we can call the function. It shuffles elements each time when the function calls and produces different output.
Example:
-
- import random
-
- sample_list1 = ['Z', 'Y', 'X', 'W', 'V', 'U']
- print("Original LIST1: ")
- print(sample_list1)
-
- random.shuffle(sample_list1)
- print("\nAfter the first shuffle of LIST1: ")
- print(sample_list1)
-
- random.shuffle(sample_list1)
- print("\nAfter the second shuffle of LIST1: ")
- print(sample_list1)
Output:
Original LIST1:
['Z', 'Y', 'X', 'W', 'V', 'U']
After the first shuffle of LIST1:
['V', 'U', 'W', 'X', 'Y', 'Z']
After the second shuffle of LIST1:
['Z', 'Y', 'X', 'U', 'V', 'W']