The Python random.choice() method is used to pick a random item from a sequence. This sequence can be a list, a tuple, a string or other ordered collection of elements. Other sequences like sets are not accepted as arguments to this method and a TypeError is raised. This is because sets are considered as unordered sequences and do not record the element positions within them. Hence, they are not sub-scriptable.
The choice() method comes in handy while working on randomized algorithms or programs.
Note − This function is not accessible directly, so we need to import the random module and then we need to call this function using random static object.
SyntaxFollowing is the syntax for the Python random.choice() method −
random.choice(seq)Parameters
seq − This is an ordered collection of elements (like list, tuple, string or dictionary).
This method returns a random item.
Example 1The following example shows the usage of the Python random.choice() method.
import random #imports the random module # Create a list, string and a tuple lst = [1, 2, 3, 4, 5] string = "Tutorialspoint" tupl = (0, 9, 8, 7, 6) dct = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'} # Display the random choices from the given sequences print("Random element in a given list", random.choice(lst)) print("Random element in a given string", random.choice(string)) print("Random element in a given tuple", random.choice(tupl)) print("Random element in a given dictionary", random.choice(dct))
When we run above program, it produces following result −
Random element in a given list 1 Random element in a given string u Random element in a given tuple 8 Random element in a given dictionary b
If the same program is executed again, the result differs from the first output −
Random element in a given list 2 Random element in a given string t Random element in a given tuple 0 Random element in a given dictionary dExample 2
This method can be used to choose an element within a range of elements using the range() function.
In the following example, an integer range is returned by passing two integer arguments to the range() function; and this range is passed as an argument to the choice() method. As a return value, we are trying to retrieve a random integer from this range.
import random # Pass the range() function as an argument to the choice() method item = random.choice(range(0, 100)) # Display the random item retrieved print("The random integer from the given range is:", item)
The output for the program above is obtained as follows −
The random integer from the given range is: 14Example 3
However, if we create and pass other sequence objects like sets to the choice() method, a TypeError is raised.
import random #imports the random module # Create a set object 's' s = {1, 2, 3, 4, 5} # Display the item chosen from the set print("Random element in a given set", random.choice(s))
Traceback (most recent call last): File "main.py", line 5, in <module> print("Random element in a given set", random.choice(s)) File "/usr/lib64/python3.8/random.py", line 291, in choice return seq[i] TypeError: 'set' object is not subscriptableExample 4
And when we pass an empty sequence as an argument to the method, an IndexError is raised.
import random # Create an empty list seq seq = [] # Choose a random item item = random.choice(seq) # Display the random item retrieved print("The random integer from the given list is:", item)
Traceback (most recent call last): File "main.py", line 7, in <module> item = random.choice(seq) File "/usr/lib64/python3.8/random.py", line 290, in choice raise IndexError('Cannot choose from an empty sequence') from None IndexError: Cannot choose from an empty sequence
python_modules.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4