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
44 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

+1 vote
4 answers

Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

answered Oct 18, 2018 in Python by tinitales
6,488 views
0 votes
1 answer

Python program to read the excel file which contains more than 65K records

Hi @Rashmi! I read your code and there ...READ MORE

answered Feb 8, 2019 in Python by Omkar
• 69,060 points
138 views
0 votes
1 answer
+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 962 views
0 votes
0 answers
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
101 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 23, 2018 in Python by aryya
• 7,350 points
803 views