Randomly select elements from list (array) in python

You can get one element randomly from the array by using the choice function of the random module.

However, note that if the number of elements is 0, an Exception will be raised.

>>> import random
>>> random.choice(["hoge","fuga","piyo"])
'piyo'
>>> random.choice(["hoge","fuga","piyo"])
'fuga'
>>> random.choice([])
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/lib64/python2.7/random.py", line 275, in choice
    return seq[int(self.random() * len(seq))]  # raises IndexError if seq is empty
IndexError: list index out of range

Recommended Posts

Randomly select elements from list (array) in python
[Python] Manipulating elements in a list (array) [Sort]
Getting list elements in Python
Extract every n elements from an array (list) in Python and Ruby
[Python] Manipulation of elements in list (array) [Add / Delete]
Delete multiple elements in python list
Download images from URL list in Python
[Python] Combine all the elements in the array
Sorted list in Python
Sorted list in Python
Filter List in Python
List find in Python
Sort list elements in a specified order in Python
How to remove duplicate elements in Python3 list
Extract elements (using a list of indexes) in a NumPy style from a Python list / tuple
[Python] Outputs all combinations of elements in the list
Quicksort an array in Python 3
Empty multidimensional array in python
Make sure all the elements in the list are the same in Python
How to slice a block multiple array from a multiple array in Python
How to swap elements in an array in Python, and how to reverse an array.
Sort and output the elements in the list as elements and multiples in Python.
Get the number of specific elements in a python list
Allow Python to select strings in input files from folders
Extract multiple list duplicates in Python
python web scraping-get elements in bulk
Difference between list () and [] in Python
[python] Manage functions in a list
Output 2017 Premium Friday list in Python
Get data from Quandl in Python
python / Make a dict from a list.
Extract text from images in Python
Python3> List generation from iterable> list (range (5))
Extract strings from files in Python
[Python of Hikari-] Chapter 05-03 Control syntax (for statement-extracting elements from list-)
Various ways to create an array of numbers from 1 to 10 in Python.
Get exchange rates from open exchange rates in Python
Display a list of alphabets in Python 3
Revived from "no internet access" in Python
Prevent double boot from cron in Python
OR the List in Python (zip function)
Python / numpy> list (numpy array) file save / load
Tips: [Python] Randomly restore and extract an array from a fasta file
Difference between append and + = in Python list
[Python] Display list elements with variadic arguments
Get battery level from SwitchBot in Python
Join List elements without''. (Retrieve String from list without'')
Summary of built-in methods in Python list
Generate a class from a string in Python
[Python] list
Generate C language from S-expressions in Python
Get the EDINET code list in Python
Convert from Markdown to HTML in Python
Get Precipitation Probability from XML in Python
[Python] Understand list slicing operations in seconds
Get metric history from MLflow in Python
Get time series data from k-db.com in Python
[Python] Sort the list of pathlib.Path in natural sort
How to clear tuples in a list (Python)
Try implementing two stacks in one array in Python
Create a C array from a Python> Excel sheet