119

The first examples that I googled didn't work. This should be trivial, right?

Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101
Hanno Fietz
  • 30,799
  • 47
  • 148
  • 234
  • 5
    This was in my first hit: http://docs.python.org/library/random.html#module-random -- random.choice(seq)¶ Return a random element from the non-empty sequence seq. – Daniel Sloof Jun 29 '09 at 14:52
  • 1
    Many times looking in the library can be more helpful. Getting the documentation for the random module would have worked. It does take some time to know where to look, but for anything involving "random" check the random module first. – Kathy Van Stone Jun 29 '09 at 15:18

2 Answers2

240
import random
random.choice (mylist)
eduffy
  • 39,140
  • 13
  • 95
  • 92
62
import random
random.choice([1, 2, 3])
Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101