Skip to content
Sign UpLog In
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs
1

how can i select a random value from a list?

TheImagin33r
TheImagin33r

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

2 years ago

Voters

Comments

TopNew
4
RYANTADIPARTHI
RYANTADIPARTHI

Solution

use the random module. Here's how.

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

like that.
That should work

2 years ago
1

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

2 years ago
1
Kookiez
Kookiez

What language is this in?

2 years ago
1
programmeruser
programmeruser

What language is this?

2 years ago
0
mesquite2234
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
2 years ago
Load more