4

Possible Duplicate:
Shuffling a list of objects in python

IF I have a list:

a = ["a", "b", "c", ..., "zzz"]

how can I randomly shuffle its elements in order to obtain a list:

b = ["c", "zh", ...]

without consuming a lot of the system's resources?

Community
  • 1
  • 1
relima
  • 3,439
  • 5
  • 31
  • 53
  • Exact duplicate: http://stackoverflow.com/questions/976882/shuffling-a-list-of-objects-in-python – Mike Oct 17 '10 at 17:19

3 Answers3

9
import random
b = list(a)
random.shuffle(b)
Matthew Flaschen
  • 268,153
  • 48
  • 509
  • 534
5

random.shuffle() shuffles a sequence in-place.

Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
2

Not sure how much resources it consumes, but shuffle in the random module does exactly like this.

import random
a = [1,2,3,4,5]
random.shuffle(a)
jlv
  • 617
  • 8
  • 13