Python 3 How to Shuffle List Examples

In this post you can find everything related to shuffling lists in Python. All the examples are tested against Python 3.7. Some of the examples include free shuffling, synchronized shuffling of several lists with seed, shuffling different types of lists. The examples are for beginners to advanced and include full code and results:

You may like also: Python Random Number Examples

Python shuffle list of numbers / range

Shuffling a list in Python the numbers from 0 to 20 (exclusive 20) generated by range. Generating list of numbers with ranges is a common operation in Python. The only disadvantage is that they are sorted. If you want to shuffle the list in random order you can use random.shuffle :

import random
nums = [x for x in range(20)]
random.shuffle(nums)
print(nums)

result:

[9, 15, 19, 16, 8, 17, 10, 1, 14, 4, 18, 13, 0, 5, 12, 3, 7, 11, 6, 2]

Repeating the shuffle will produce different result each time unless we use a seed.

Python shuffle list of numbers

Shuffling a list in python the numbers from predefined list. The example is identical to the previous one:

import random
nums = [0, 5, 7, 4, 6, 8, 3]
random.shuffle(nums)
print(nums)

result:

[5, 8, 4, 7, 3, 0, 6]

Python shuffle list of strings

The same way can be applied for list of strings. All you need to do is use method - random.shuffle:

import random
str = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
random.shuffle(str)
print(str)

result:

['f', 's', 'c', 'x', 'a', 'l', 'n', 'z', 'e', 'r', 'j', 'v', 'w', 'g', 'm', 'u', 'k', 'd', 'i', 'p', 'h', 'q', 'y', 'b', 't', 'o']

If you want to sort the shuffled list you have two options:

  • change the list
str.sort()
  • keep the original list
for x in sorted(str):
    print x

Python shuffle array with seed

Sometimes you will need to use a seed in order to get special order or if you need to have different level of shuffling. In order to verify shuffling and seed you need to have two list and one seed. This is a basic example showing only how to use seed for one list:

import random

SEED = 'abc'

array = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
random.seed(SEED)
random.shuffle(array)

print ( array)

result with seed:

['x', 'n', 'y', 'u', 's', 'l', 'i', 'm', 'k', 'c', 'f', 'h', 'r', 'z', 'w', 'b', 'o', 'p', 't', 'e', 'd', 'g', 'v', 'j', 'q', 'a']

result without seed:

['u', 'e', 'y', 'b', 'c', 'm', 'g', 't', 'o', 'k', 'v', 'z', 'p', 'w', 'q', 'x', 'n', 'l', 'r', 's', 'a', 'f', 'j', 'd', 'h', 'i']

Python shuffle list of lists

Shuffling list of lists in python with loop. This will shuffle not only the first level of list elements but also the nested ones. This code works only with two levels of sublists but the same approach can be applied for more levels:

import random

mylist = [['a','b','c'],['d','e','f'],['g','h','i']]
random.shuffle(mylist)
for sublist in mylist:
    random.shuffle(sublist)
print(mylist)

result:

[['h', 'g', 'i'], ['b', 'c', 'a'], ['f', 'e', 'd']]

Related Article