How to randomly select an item from a list

0 votes

Assume I have the following list:

foo = ['a', 'b', 'c', 'd', 'e']

What is the simplest way to retrieve an item at random from this list?

Dec 2, 2020 in Python by Roshni
• 10,480 points
89 views

1 answer to this question.

0 votes

Use random.choice()

import random

foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
answered Dec 2, 2020 by Gitika
• 65,910 points

Related Questions In Python

0 votes
1 answer

How to have values from a List1 into Columns Names and List 2 Values as Index for an Empty DataFrame

Try like this, it will give you ...READ MORE

answered Jul 10, 2019 in Python by Cherukuri
• 32,970 points
314 views
0 votes
1 answer
0 votes
4 answers

How do I remove an element from a list by index in Python?

1886 Use del and specify the index of the element ...READ MORE

answered Dec 11, 2020 in Python by Gitika
• 65,910 points
78,327 views
+1 vote
1 answer

How to convert records from csv to a list in Python?

If you are using Python 3.x then ...READ MORE

answered Jun 25, 2019 in Python by Arvind
• 3,000 points

edited Jun 26, 2019 by Kalgi 6,092 views
0 votes
2 answers
+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,183 views
0 votes
0 answers
0 votes
1 answer

How to find the index of an item in a list?

One thing that is really helpful in ...READ MORE

answered Dec 3, 2020 in Python by Gitika
• 65,910 points
268 views
0 votes
1 answer

How to remove an element from a list by index?

Use del and specify the index of the element ...READ MORE

answered Dec 18, 2020 in Python by Gitika
• 65,910 points
80 views