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"???)
2
  • 2
    It's definitely a duplicate. Commented Apr 24, 2017 at 0:07
  • 1
    The accepted answer here is the same as the accepted answer given in the question linked above. Commented Apr 24, 2017 at 0:27

2 Answers 2

6
import random
items = ["random1", "random2", "random3"]
random.choice(items)
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.