
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Random range in a List
When it is required to find the random range in a list, a list comprehension and the ‘randrange’ method present in ‘random’ package is used.
Example
Below is a demonstration of the same −
import random my_result = [random.randrange(1, 100, 1) for i in range(10)] print ("The result is :") print(my_result)
Output
The result is : [40, 73, 58, 45, 68, 19, 86, 6, 15, 71]
Explanation
The required packages are imported into the environment.
A list comprehension is used to iterate over the list, and 'randrange' method is used to generate random number in the given range.
This result is assigned to a variable.
This is the output that is displayed on the console.
- Related Articles
- Python Generate random numbers within a given range and store in a list
- Python program to generate random numbers within a given range and store in a list?
- Generating random number list in Python
- Python program to Reverse a range in list
- Alternate range slicing in list (Python)
- Java program to generate random numbers within a given range and store in a list
- Python Front and rear range deletion in a list?
- Copy list with random Pointer in Python
- Generating random number in a range in C
- Find missing numbers in a sorted list range in Python
- How to pick a random number not in a list in Python?
- Assign range of elements to List in Python
- Create list of numbers with given range in Python
- Generating n random numbers between a range - JavaScript
- Python program to extract characters in given range from a string list

Advertisements