Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Shuffle/Randomize A List In Python

### Shuffle A List In Place

The basic way to shuffle a list is :

python
#!/usr/bin/env python3

import random

list = ['a', 'b', 'c', 'd', 'e']

random.shuffle(list)

print(list)

The list is shuffled in place and the output of the above script will be something like :

['c', 'e', 'a', 'b', 'd']

### Shuffle Items Into A New List

If you want to keep the original list in place, you can use [TODO: Code shorthand span ] to make a new list with the shuffled contents like this :

python
#!/usr/bin/env python3

import random

original_list = ['a', 'b', 'c', 'd', 'e']

new_list = random.sample(original_list, len(original_list))

print(original_list)
print(new_list)