On this page
random.shuffle
Prev: random.setstate | Up: random | Next: random.SystemRandom
Rel: random.betavariate, random.binomialvariate, random.choice, random.choices
Ref: docs.python.org — random.shuffle
Tags: #public #python3 #stdlib #random
Import
import random
from random import shuffle
Signature & Overview
random.shuffle(x)
Shuffle list x in place, and return None.
Usage
import random
rng = random.Random(1)
deck = [1, 2, 3, 4, 5]
rng.shuffle(deck)
assert sorted(deck) == [1, 2, 3, 4, 5]
Gotchas
- Shuffles in place and returns
None: Don't writedeck = random.shuffle(deck)—userandom.sample(seq, len(seq))to get a new shuffled list without mutating the original.
REPL Inspect
from random import shuffle
help(shuffle)
Linked from
Related notes
- tuts/python3/stdlib_module/random/setstate Linked here; links to this page; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/SystemRandom Linked here; links to this page; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/betavariate Linked here; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/binomialvariate Linked here; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/choice Linked here; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/choices Linked here; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/random Linked here; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/random/expovariate shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/gammavariate shared tags: #random, #stdlib