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,462
  • 5
  • 34
  • 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
  • 278,309
  • 50
  • 514
  • 539
5

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

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
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