Thursday 23 September 2021

Python: Shuffle a list, sequence

'random.shuffle' method is used to shuffle a list, sequence.

 

Signature

random.shuffle(x[, random])

 

Example

random.shuffle(my_list)

 

shuffle_list.py

import random

primes = [2, 3, 5, 7, 11, 13]

print('Before shuffling, primes -> ', primes)

random.shuffle(primes)
print('After shuffling, primes -> ', primes)

 

Output

Before shuffling, primes ->  [2, 3, 5, 7, 11, 13]
After shuffling, primes ->  [11, 5, 13, 7, 2, 3]

 


Previous                                                    Next                                                    Home

No comments:

Post a Comment