Here we will learn about how to randomly select from the list in python. We will learn some of the popular ways. Some of the methods are familiar to us but some of them are new to us. We can get random elements using a random module. In this article, we will learn How to select a single random and multiple random elements. With that, we also learn how to select random elements without repetition.
The random is a built-in module and that is useful to get random elements in a list, set, or tuple. This module is useful to perform some tasks like selecting random numbers and shuffling the numbers. random module contains different types of functions like random.randint(), random.choice(), etc.
Select a random element from a list
There are many methods available to get a random element from a list. First, we will learn how to get a single random element from a list. For that, we are using some methods like random.choice(), random.randint(), random.randrange(), and secret module. These are the ways to get only one random element from a list.
1. Using random.choice() Method to Randomly Select from list in Python
This method returns a random element from a list, set, or tuple.
Syntax
random. choice(sequence)
Parameter
sequence: A sequence may be a list, tuple, or set.
Returns
random element
Get a random element from list using random.choice()
import random
list1=[1,'a','p',4,'t',5,6,'i',7]
print("Random element is :",random.choice(list1))
Import a random module. Now create a list with elements and integers. Next using random.choice() to get a random element from the list.
Output
Random element is : 7
2. Using random.randint() Method Randomly Select from list in Python
random.randint() is one of the methods in a random module. It is useful to get a random element from the specified range.
Syntax
random. randint(start,stop)
Parameters
- start – starting range
- stop – end value
Returns
random element
Get a random element from list using random.randint()
import random
list1=[1,'a','p',4,'t',5,6,'i',7]
print("Random element is :",random.randint(0,len(list1)-1))
Import a random module. Now create a list with elements and integers. Next using random.randint() to get a random element from the list.
Output
Random element is : 3
Recommended Reading | 9 Unique Numpy Random Functions to Create Random Data
3. Using random.randrange() Method to Randomly Select from list in Python
random.randrange() is one of the methods in a random module. It is useful to get a random element from the specified range. If the start value is not specified it will take the value from 0.
Syntax
random.randrange(start, stop, step)
Parameters
- start – start value of the range.
- stop – end value of the range
- step – increment value
Returns
random element
Get a random element from a list using random.randrange()
import random
list1=[1,'a','p',4,'t',5,6,'i',7]
print("Random element is :",random.randrange(0,len(list1)-1))
Import a random module. Now create a list with elements and integers. Next using random.randrange() to get a random element from the list.
Output
Random element is : 6
4. Using Secret Module Randomly Select from list in Python
A secret is a module that is in-built in python. It is useful to get a random number in python.
Syntax
secret.choice(sequence)
Parameter
sequence: list, set, or tuple
Returns
random element
Get a random element from a list using secret module
import secrets
list1 = [1,'a','p',4,'t',5,6,'i',7]
print("Random element is :",secrets.choice(list1))
Import a secret module. Now create a list with elements and integers. Next using secret.choice() to get a random element from the list.
Output
Random element is : i
Select multiple random elements from a list
Till we saw how to get a single random element. Now we will move on to the multiple random elements. To get multiple random elements we are using random.sample(), and random.choices(). These are the methods to get more than one random element.
5. By random.sample() method
random.sample() is a built-in function in python.random module holds the function random.sample(). It is useful to select multiple elements from a list.
Syntax
random.sample(sequence, k)
Parameters
- sequence: can be list, tuple, or set
- k : number of random elements we want
Returns
multiple random elements
Get random elements from a list using random.sample
import random
list1 = [1,'a','p',4,'t',5,6,'i',7]
print("Random element are :",random.sample(list1, 5))
Import a random module. Now create a list with elements and integers. Next using a random.sample() to get random elements from the list.
Output
Random element are : [1, 5, 6, 'i', 'a']
6. Using random.choices() Method to Randomly Select from list in Python
random.choices() is a built-in function in python. The random module holds the function random.choices(). It is useful to select multiple elements from a list or a single element from a given sequence.
Syntax
random.choices(sequence, weights, cum_weights, k)
Parameters
- sequence – list, tuple, or string
- weights – relative weight
- cum_weights – cumulative weight
- k – length
Returns
random multiple elements
Get random elements from a list using random.choices
import random
list1 = [1,'a','p',4,'t',5,6,'i',7]
print("Random elements are :",random.choices(list1, k=4))
Import a random module. Now create a list with elements and integers. Next using random.choices() to get random elements from the list.
Output
Random elements are : [5, 'i', 7, 5]
Difference Between random.choice() and random.choices()
random.choice() | random.choices() |
---|---|
This is useful to generate a single random element from a given sequence | This is useful to generate a single as well as multiple random elements from a given sequence |
It only takes one positional argument. | It takes more than one positional argument |
The output will be in a normal form | The output will be in a given sequence |
Bonus: How to get random elements without repetition
import random
def random_element(list1, n):
random.shuffle(list1)
new_lst = []
for i in range(0, len(list1), n):
new_lst.append(list1[i:i + n])
return new_lst
list1 = [1,'a','p',4,'t',5,6,'i',7,9]
print(random_element(list1, 2))
- Import a random module.
- Create a function named random_element.
- Now, using random.shuffle() to shuffle all the elements in a list.
- Create a empty list named new_list.
- Now creating for loop to iterate.
- Using append() function to add elements in a new list.
- Next returning new_lst.
- Finally, declaring a list to get a random elements without repeatition.
Output
[['t', 9], [1, 7], [4, 'a'], ['p', 'i'], [5, 6]]
FAQs Related to Randomly Select From List in Python
The output will be in the form of a list. Because the input given is a list.
No, we don’t get multiple random elements using random.choice().
Final Thoughts
Here we came to the end of the article. In this article, we learned about how to get random elements in a list. We hope this article was interesting and easy to learn. random is a module that is useful to get random elements.
Try to solve all the programs on your own. If there are any methods available mention them in the comments.