How to randomly select item from a list in Python

3 Answers

0 votes
import random

def getItem(lst):
  return random.choice(lst)
  
lst = ['python', 'java', 'php', 'swift', 'c++', 'c', 'c#']

print(getItem(lst))

 
 
 
 
 
'''
run:
 
c
 
'''

 



answered Apr 19, 2021 by avibootz
0 votes
import secrets

def getItem(lst):
  return secrets.choice(lst)
  
lst = ['python', 'java', 'php', 'swift', 'c++', 'c', 'c#']

print(getItem(lst))

 
 
 
 
 
'''
run:
 
java
 
'''

 



answered Apr 19, 2021 by avibootz
0 votes
import numpy 

def getItem(lst):
  return numpy.random.choice(lst)
  
lst = ['python', 'java', 'php', 'swift', 'c++', 'c', 'c#']

print(getItem(lst))

 
 
 
 
 
'''
run:
 
c
 
'''

 



answered Apr 19, 2021 by avibootz

Related questions

2 answers 213 views
1 answer 132 views
1 answer 141 views
1 answer 192 views
1 answer 118 views
...