0 votes
in Python by
Give an example of shuffle() method?

1 Answer

0 votes
by

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:

  1. # import the random module  
  2. import random  
  3. # declare a list  
  4. sample_list1 = ['Z''Y''X''W''V''U']  
  5. print("Original LIST1: ")  
  6. print(sample_list1)  
  7. # first shuffle   
  8. random.shuffle(sample_list1)  
  9. print("\nAfter the first shuffle of LIST1: ")  
  10. print(sample_list1)  
  11. # second shuffle  
  12. random.shuffle(sample_list1)  
  13. print("\nAfter the second shuffle of LIST1: ")  
  14. 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']

Related questions

0 votes
asked Aug 22, 2022 in Python by Robindeniel
0 votes
asked Nov 17, 2023 in Azure by GeorgeBell
...