Python Randomly Select From List
Generating random values or selecting random items from a collection is an essential task in programming. This can be useful for creating simulations, games, tests, or any other scenario where randomness is needed. In Python, there are several ways to randomly select from a list such as using the “random.choices()”, or “random.sample()” functions, etc.
How to Randomly Select/Choose From a List in Python?
The below methods are utilized in Python to randomly select an item/element from a list:
Method 1: Randomly Select From a List Using the “random.choice()” Function
In Python, the “random.choice()” function is used to select or return a random item/element from a non-empty sequence value.
Syntax
In the above syntax, “seq” indicates any sequence value such as list, tuple, or str.
Example
The following code is used to select a random item/element from the initialized list:
list1 = [45, 66, 88, 19, 32]
print(random.choice(list1))
In the above code, the “random.choice()” function takes the defined list as an argument and retrieves the random list item.
Output
The random element has been selected from the input list.
Method 2: Randomly Select From a List Using the “random.randrange()” Method
The “randrange()” method of the “random” module is utilized to select a random item/element from the specified range. This can be employed to randomly select from a list.
Syntax
Here:
- The “x” parameter specifies the start value.
- The “y” parameter indicates the end/last value of the specified range.
- The “z” parameter indicates the step or increment value between the specified range.
Example
The below code is utilized to randomly select from the input list:
list1 = [45, 66, 88, 19, 32]
random_index = random.randrange(len(list1))
print(list1[random_index])
According to the above code, the “random.randrange()” method takes the total list length and selects a random integer value based on that. The fetched random integer value is used inside the square notation to return the corresponding element value from the list.
Output
We have successfully selected a random element.
Method 3: Randomly Select From a List Using the “random.sample()” Method
In Python, the “random.sample()” method retrieves the specified length of a random item from the input sequence such as a list.
Syntax
Here, “seq” denotes the sequence such as list, tuple, etc. and “x” denotes the sample length as an integer value.
Example
In this code the “random.sample()” method is utilized to randomly select from the input list:
list1 = [45, 66, 88, 19, 32]
print('Select "1" Random Element: ', random.sample(list1, 1))
print('Select "3" Random Element: ', random.sample(list1, 3))
In these code lines, the “random.sample()” method takes the defined list named “list1” and a number of elements to be selected from the list as its arguments, respectively. The method then retrieves a particular number of random elements/items from the list.
Output
The single and multiple random elements have been selected accordingly.
Method 4: Randomly Select From a List Using the “random.shuffle()” Function
The “random.shuffle()” function is utilized to shuffle the input sequence by reorganizing the element. This method can be utilized to randomly select items from the list.
Syntax
Example
Go through the below-given example code:
list1 = [45, 66, 88, 19, 32]
random.shuffle(list1)
print(list1[0])
In the above example, the “random.shuffle()” function takes the input list and re-organizes it via shuffling. After shuffling, the “square bracket” notation is used to access the first element from the list that becomes random after shuffling.
Output
As seen, the random list element is retrieved against the first index.
Conclusion
The “random.choice()”, “random.randrange()”, “random.sample()”, or the “random.shuffle()” method is used to randomly select an item from the list in Python. This tutorial presented various ways to randomly select an item/element from the list using numerous examples.
Source: linuxhint.com