Python Program to Randomly Select an Element From the List – 2022

Machine Learning Projects

Hey guys, in this blog we will see a Python Program to Randomly Select an Element From the List.

Example 1: Using random module

import random

my_list = ['machine', 'learning', 'projects', 1, 2, 3]
print(random.choice(my_list))

Output

projects
  • random.choice() is used to choose an element at random from the provided list.

Example 2: Using the secrets module

import secrets

my_list = ['machine', 'learning', 'projects', 1, 2, 3]
print(secrets.choice(my_list))

Output

2

Check out our other python programming examples

Leave a Comment

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