In this article, we will discuss how to randomly select n elements from the list in Python. Before moving on to the approaches let’s discuss Random Module which we are going to use in our approaches.
Random module is the predefined Module in Python, It contains methods to return random values. This module is useful when we want to generate random values. Some of the methods of Random module are:-
Let’s discuss different approaches to implement this.
Approach 1 : Using
sample() method is used to return required list of items from a given sequence. This method do not allow duplicate elements in a sequence.
[8, 6, 6, 4]
Approach 2 : Using
choice() method is used to return a random number from given sequence. The sequence can be a list or a tuple. This returns a single value from available data that considers duplicate values in the sequence(list).
8 2 4 6
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Randomly select elements from list without repetition in Python
- How to randomly select rows from Pandas DataFrame
- Divide a Pandas DataFrame randomly in a given ratio
- Python | Select random value from a list
- Python program to select Random value form list of lists
- Select Drop-down list using select_by_index() in Selenium - Python
- Python | Pandas Series.select()
- Python | Select dictionary with condition given key greater than k
- Python MySQL - Select Query
- Select any row from a Dataframe in Pandas | Python
- numpy.select() function | Python
- Select first or last N rows in a Dataframe using head() and tail() method in Python-Pandas
- How to select a drop-down menu value using Selenium in Python?
- Python MariaDB - Select Query using PyMySQL
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- How to select multiple columns in a pandas dataframe
- Select row with maximum and minimum value in Pandas dataframe
- Select any row from a Dataframe using iloc and iat in Pandas
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.