Use random.choice()
:
import random foo = ['a', 'b', 'c', 'd', 'e'] print(random.choice(foo))
For cryptographically secure random choices (e.g., for generating a passphrase from a wordlist), use secrets.choice()
:
import secrets foo = ['battery', 'correct', 'horse', 'staple'] print(secrets.choice(foo))
secrets
is new in Python 3.6. On older versions of Python you can use the random.SystemRandom
class:
import random secure_random = random.SystemRandom() print(secure_random.choice(foo))
Related Posts:
- Python: Array v. List
- How can I reverse a list in Python?
- Difference between del, remove, and pop on lists
- How can I reverse a list in Python?
- What is the difference between Python’s list methods append and extend?
- Python – TypeError: ‘int’ object is not iterable
- Understanding slice notation
- Understanding slice notation
- TypeError: list indices must be integers or slices, not str
- IndexError: list index out of range and python
- Python – TypeError: ‘int’ object is not iterable
- How to convert list to string [duplicate]
- How do I get the number of elements in a list?
- Finding the index of an item in a list
- Finding the index of an item in a list
- Accessing the index in ‘for’ loops?
- How to make a flat list out of a list of lists
- Removing duplicates in lists
- What does the list() function do in Python?
- List changes unexpectedly after assignment. Why is this and how can I prevent it?
- Why does this iterative list-growing code give IndexError: list assignment index out of range?
- TypeError: ‘float’ object is not subscriptable
- What does random.sample() method in python do?
- Is there a short contains function for lists?
- TypeError: ‘float’ object is not subscriptable
- Transpose/Unzip Function (inverse of zip)?
- appending list but error ‘NoneType’ object has no attribute ‘append’
- Random string generation with upper case letters and digits
- Check if something is (not) in a list in Python
- Python: finding an element in a list [duplicate]
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- Finding the average of a list
- Writing a list to a file with Python
- How do I concatenate two lists in Python?
- if else in a list comprehension
- Print a list in reverse order with range()?
- Get unique values from a list in python [duplicate]
- Fastest way to check if a value exists in a list
- How can I count the occurrences of a list item?
- TypeError: ‘list’ object cannot be interpreted as an integer
- Why is it string.join(list) instead of list.join(string)?
- Why is it string.join(list) instead of list.join(string)?
- How do I concatenate two lists in Python?
- if else in a list comprehension
- Finding median of list in Python
- Python set to list
- Python List vs. Array – when to use?
- How to find all occurrences of an element in a list
- Get list from pandas dataframe column or row?
- Finding and replacing elements in a list
- How to find all the indexes of a recurring item in a list?
- How to get a random number between a float range?
- ValueError: max() arg is an empty sequence
- Remove list from list in Python
- python – if not in list
- How do I concatenate two lists in Python?
- How can I compare two lists in python and return matches
- Replace values in list using Python
- Append integer to beginning of list in Python
- List comprehension on a nested list?
- Union of two lists in Python
- Get unique values from a list in python
- ‘list’ object has no attribute ‘shape’
- Writing a list to a file with Python
- How do I check if a list is empty?
- Python: Get the first character of the first string in a list?
- Understanding slice notation
- How do I find the duplicates in a list and create another list with them?
- Add list to set?
- What’s the idiomatic syntax for prepending to a short python list?
- How to remove an element from a list by index
- Apply function to each element of a list
- Generate random sentences in python
- TypeError: unsupported operand type(s) for -: ‘list’ and ‘list’
- Remove all occurrences of a value from a list?
- Shuffle an array with python, randomize array item order with python
- Converting Dictionary to List?
- Python Sets vs Lists
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- How to deep copy a list?
- Python return list from function
- TypeError: list indices must be integers or slices, not str
- use np.random.multinomial() in python
- How to check if a string is a substring of items in a list of strings?
- In Python, when to use a Dictionary, List or Set?
- Whats the difference between ‘for x in list:’ and ‘for x in list[:]:’
- Removing Punctuation From Python List Items
- Check if two unordered lists are equal
- ‘list’ object has no attribute ‘shape’
- How do I find the duplicates in a list and create another list with them?
- How can I compare two lists in python and return matches
- How do I check if a list is empty?
- How can I get list of values from dict?
- IndexError: list index out of range and python
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- How to sort a list of objects based on an attribute of the objects?
- Pop index out of range
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character