1

i was curious on how to randomly select an item from a list? for example you want to randomly choose an item from a list to print something. In the example code below i make a list with random items in it and i want the print to say "the random item is" and then a random item from the list. Sorry if this is a bit unclear if you want me to explain it better just ask :) Thanks

#Example
random = ["random1", "random2", "random3"]
print("Random item is " + "Random Item"???)
xStrqfe
  • 81
  • 2
  • 8

2 Answers2

6
import random
items = ["random1", "random2", "random3"]
random.choice(items)
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
  • 1
    Please do a search for duplicates before answering common questions like this. FWIW, the question linked by juanpa.arrivillaga already had 37 duplicate links, we really don't need another one. – PM 2Ring Apr 24 '17 at 05:47
0
import random
list = ["random1", "random2", "random3"]
random.choice(list)

The random.choice(list) returns one of the items in the list, which means you can do this:

import random
list = ["random1", "random2", "random3"]
chosen = random.choice(list)
print("The chosen was: {}".format(chosen))
Invision
  • 90
  • 1
  • 10