Select randomly n elements from a list using choice()

The 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). 

python3




# importing random module
import random
 
# declaring list
list = [2, 2, 4, 6, 6, 8]
 
# initializing the value of n
n = 4
 
# traversing and printing random elements
for i in range(n):
     
    # end = " " so that we get output in single line
    print(random.choice(list), end = " ")


Output : 

8 2 4 6 


Randomly select n elements from list in Python

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. 

A random module returns random values. It is useful when we want to generate random values. Some of the methods of Random module are:- seed(), getstate(), choice(), sample() etc.

Similar Reads

Select randomly n elements from a list using randrange()

Here, we are using the random randrange() function to return a single random number from a list....

Selecting more than one random element from a list using sample()

...

Selecting more than one random element from a list using choices()

The sample() method is used to return the required list of items from a given sequence. This method does not allow duplicate elements in a sequence....

Select randomly n elements from a list using choice()

...