Randomly Select Element From List in Python Using sample()

(Last Updated On: 03/03/2023)

Python Program to Randomly Select Element From List Using sample()

# Randomly Select an more than one Element From the List using sample() in python

# importing random module
import random
 
# declaring list
list = [2, 2, 4, 6, 6, 8]
 
# initializing the value of n
n = 6
 
# printing n elements from list
print(random.sample(list, n))

Output:

[2, 2, 8, 6, 6, 4]

Leave a Reply

Your email address will not be published. Required fields are marked *