Select more than one item randomly from the list

0 votes
I have a list and I would like to select n items randomly from he list. How do I do that in python?
May 28, 2019 in Python by Tejas
863 views

1 answer to this question.

0 votes

You can use random.sample() function in the following way:

import random
group_of_items = {1, 2, 3, 4}              
num_to_select = 2                          
list_of_random_items = random.sample(group_of_items, num_to_select)
first_random_item = list_of_random_items[0]
second_random_item = list_of_random_items[1] 
answered May 28, 2019 by Yesha

Related Questions In Python

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,890 points
697 views
0 votes
1 answer

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Hello @kartik, Since different users might have different ...READ MORE

answered Jun 15, 2020 in Python by Niroj
• 82,880 points
23,183 views
0 votes
0 answers

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I couldn't figure out the problem. here my ...READ MORE

Dec 17, 2020 in Python by muammer
• 120 points
7,654 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 4,348 views
0 votes
1 answer
0 votes
2 answers

Pick a random item from the list - Python

You can also use secrets.choice() import secrets list = ...READ MORE

answered May 28, 2019 in Python by Kim
1,270 views
0 votes
1 answer

Is there anyway to obtain the full abstract from a 'PUBmed' article using bioPython

Hey Charlie, it's certainly possible to pull ...READ MORE

answered Aug 24, 2018 in Python by aryya
• 7,460 points
3,231 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