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
5,314 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
3,473 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,770 points
756 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,720 points
1,425 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 24, 2018 in Python by charlie_brown
• 7,720 points
1,286 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
1,501 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,560 points
1,153 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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,454 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP