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,520 points
223 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,890 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
• 33,050 points
475 views
0 votes
1 answer
0 votes
4 answers

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

Delete the List and its element: We have ...READ MORE

answered Jun 7, 2020 in Python by sahil
• 580 points
210,361 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,040 points

edited Jun 26, 2019 by Kalgi 8,581 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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 3,152 views
0 votes
1 answer
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,890 points
657 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,890 points
276 views
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