• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Select Random Element from A List in Python

Select Random Element from A List in Python

Author: Aditya Raj
Last Updated: February 18, 2022

While programming in python, we may need to select a random element from a list in several situations. In this article, we will discuss different ways to select an element from a list in python.

Select Random Element from A List  using the random module

The random module in python provides us with different function to generate random numbers.  We can also select random elements from a list using the functions defined in this module.

To select a random element from a list in python, we can use the choice() function defined in the random module. The choice() function takes a list as input and returns a random element from the list every time it is executed. 

You can observe this in the following example.

import random

myList = [1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
print("The list is:")
print(myList)
random_element = random.choice(myList)
print("The randomly selected element is:", random_element)

Output:

The list is:
[1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
The randomly selected element is: 8

Select Random Element from A List  using the secrets module

The secrets module  is used for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets. However, we can also use this module to select a random element from a list. 

The choice() function defined in the secrets module works the same way as the choice() function defined in the random module. It takes a list as input and returns an element from the list as follows.

import secrets

myList = [1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
print("The list is:")
print(myList)
random_element = secrets.choice(myList)
print("The randomly selected element is:", random_element)

Output:

The list is:
[1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
The randomly selected element is: 45

Using the numpy module

We can also use the choice() function from the numpy module to select a random element from a list. The choice() function in the numpy module works the same way as random module or secrets module. You can observe this in the following example.

import numpy

myList = [1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
print("The list is:")
print(myList)
random_element = numpy.random.choice(myList)
print("The randomly selected element is:", random_element)

Output:

The list is:
[1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
The randomly selected element is: 3

While using the numpy module, we have a benefit that we can even make more than one random selection  from the list. For this, we will use the “size” parameter of the function. If we want to select n  random elements from a given list, we will pass the number n as the second input argument to the choice() function defined in the numpy module. Upon execution, the function returns a list of n elements as shown below.

import numpy

myList = [1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
print("The list is:")
print(myList)
random_elements = numpy.random.choice(myList, 4)
print("The randomly selected elements are:")
for x in random_elements:
    print(x)

Output:

The list is:
[1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
The randomly selected elements are:
78
3
6
23

Here,you can observe that an element can be selected more than once in the output list. To avoid this, we will use the third parameter i.e. “replace” and set it to False. After this, once an element is selected, it will not be considered for another selection. Hence, an element will appear only once in the output list. You can observe this in the following example.

import numpy

myList = [1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
print("The list is:")
print(myList)
random_elements = numpy.random.choice(myList, 4, replace=False)
print("The randomly selected elements are:")
for x in random_elements:
    print(x)

Output:

The list is:
[1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
The randomly selected elements are:
1
56
3
2

Conclusion

In this article, we have discussed several ways to select a random element from a list in python. We also saw how you can select more than one random elements from a list. To know more about lists in python, you can read this article on list comprehension. You might also like this article on string concatenation in python.  

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us