how can i randomly select items from a list

0 votes
can you give an example?
Apr 15, 2019 in Python by Waseem
• 4,540 points
2,949 views

2 answers to this question.

0 votes
if you google it you can find. try this:

list=['a', 'l', 'i', 'r', 'e', 'z', 'a']

from random import randint as rnd

print(list[rnd(0,len(list)-1])
answered Apr 15, 2019 by Alireza Bahrami
0 votes
You can also use the random library's choice() function.  Staying with Alireza Bahrami's earlier example list:

from random import choice as rnd_choice

list=['a', 'l', 'i', 'r', 'e', 'z', 'a']

print(rnd_choice(list))
answered Apr 9, 2020 by Patrick

Related Questions In Python

0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
2,381 views
0 votes
1 answer

How to randomly select an item from a list?

Use random.choice() import random foo = ['a', 'b', 'c', 'd', ...READ MORE

answered Dec 2, 2020 in Python by Gitika
• 65,950 points
113 views
+2 votes
2 answers

In a list of dictionaries, how can I find the minimum calue in a common dictionary field.

There are several options. Here is a ...READ MORE

answered Apr 10, 2018 in Python by charlie_brown
• 7,780 points
512 views
0 votes
1 answer

How can I get a list of locally installed Python modules?

Solution My 50 cents for getting a pip freeze-like ...READ MORE

answered May 23, 2018 in Python by charlie_brown
• 7,780 points
527 views
0 votes
2 answers

How can I get the count of a list in Python?

n=[1,2,3,4,5,6,7,8,9] print(len(n)) =9 READ MORE

answered Dec 10, 2020 in Python by anonymous
493 views
0 votes
1 answer

How can I read numbers in Python from a custom file?

Hi, good question. Let us first assume that ...READ MORE

answered Feb 6, 2019 in Python by Nymeria
• 3,520 points
484 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,365 views
0 votes
0 answers
0 votes
1 answer