Pick a random item from the list - Python

0 votes
Suppose, I have a list, is there a function which lets me pick a random item from that list? Thanks
May 28 in Python by Yamini
12 views

2 answers to this question.

0 votes

Hey @Yamini, you can choose randon.choice() for this purpose:

import random

list = ['a', 'b', 'c', 'd', 'e']
print(random.choice(list))
answered May 28 by Shashank
0 votes

You can also use secrets.choice()

import secrets
list = ['a', 'b', 'c', 'd', 'e']
print(secrets.choice(list))
answered May 28 by Kim

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
141 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
516 views
0 votes
1 answer

What is the recommended way to randomize a list of strings using Python?

Hi. Nice question. Here is the simplified answer ...READ MORE

answered Jan 18 in Python by Nymeria
• 3,500 points
38 views
0 votes
1 answer

How to use read a WSDL file from the file system using Python suds?

Hi, good question. It is a very simple ...READ MORE

answered Jan 21 in Python by Nymeria
• 3,500 points
277 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 122 views
+4 votes
6 answers
0 votes
1 answer

How to get the return value from a thread using python?

You don't need to change your existing ...READ MORE

answered Dec 3, 2018 in Python by Nymeria
• 3,500 points

edited Dec 19, 2018 by Nymeria 6,535 views
0 votes
1 answer

How can I get the count of a list in Python?

len() >>> mylist=[] >>> print len(mylist) 0 READ MORE

answered Dec 12, 2018 in Python by charlie_brown
• 7,710 points
56 views