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 in Python by Tejas
17 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 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
1,037 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 in Python by Omkar
• 67,620 points
46 views
0 votes
1 answer
+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
69 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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 278 views
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 in Python by Kim
27 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 ariaholic
• 7,340 points
337 views