Skip to content
Sign upLog in
← Back to Community

how can i select a random value from a list?

Profile icon
TheImagin33r

like if a list was like {"A","B","C"} how could i choose one at random

Voters
Profile icon
TheImagin33r
Comments
hotnewtop
Profile icon
RYANTADIPARTHI

Solution

use the random module. Here's how.

import random d = {"A","B","C"} print(random.choice(d))

like that.
That should work

Profile icon
[deleted]

Hi, you could import random
make a list.
then do random.choice(LIST_NAME_HERE)

Profile icon
mesquite2234

1: Curly brackets (in python) are for a dictionary, not a list (Lists use [])
2: The random module

import random list = ['A','B','C'] # Note [] not {} thing = random.choice(list) # thing can be anything
Profile icon
Kookiez

What language is this in?

Profile icon
programmeruser

What language is this?