Open In App

Python – Random range in list

Last Updated : 12 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a List, extract random range from it. 

Input : test_list = [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4] 
Output : [5, 7, 2, 1] 
Explanation : A random range elements are extracted of any length.

Input : test_list = [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4] 
Output : [4, 8] 
Explanation : A random range elements are extracted of any length. 

Method : Using randrange() + slicing

randrange(): Python offers a function that can generate random numbers from a specified range and also allowing rooms for steps to be included, called randrange() in random module.

In this, we extract index of list, and for first index, and then again employ the function to get end part of range using randrange() from start index to list length. The range is extracted using slicing.

Python3




# Python3 code to demonstrate working of
# Random range in list
# Using randrange() + slicing
import random
 
 
# function to Random range in list
def rabdomRangeList(test_list):
    # getting ranges
    strt_idx = random.randrange(0, len(test_list) - 1)
    end_idx = random.randrange(strt_idx, len(test_list) - 1)
 
    # getting range elements
    res = test_list[strt_idx: end_idx]
 
    return str(res)
 
 
# Driver Code
# initializing list
input_list1 = [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4]
 
# printing original list
print("\nThe original list is : ", input_list1)
 
# printing result
print("Required List : " + rabdomRangeList(input_list1))
 
 
# initializing list
input_list2 = [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4]
 
# printing original list
print("\nThe original list is : ", input_list2)
 
# printing result
print("Required List : " + rabdomRangeList(input_list2))
 
 
# initializing list
input_list3 = [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4]
 
# printing original list
print("\nThe original list is : ", input_list3)
 
# printing result
print("Required List : " + rabdomRangeList(input_list3))


Output:

The original list is :  [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4]
Required List : [19]

The original list is :  [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4]
Required List : [10, 13, 5, 7]

The original list is :  [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4]
Required List : []

Time Complexity: O(n) where n is the number of elements in the list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list 

Method: Using random.sample()

Import the random module.
Define a function randomRangeList(test_list) that takes an input list as argument.
Get the length of the input list using the len() function and store it in a variable list_len.
Generate two distinct random indices using the random.sample() function. The first argument to random.sample() should be a range object created using range(), with the start value being 0 and the stop value being list_len – 1. The second argument should be the number of random indices to generate, which is 2 in this case. Store the two indices in variables start_idx and end_idx.
Sort the two indices in ascending order using the sorted() function and store them back in start_idx and end_idx. This is necessary because the random.sample() function does not guarantee that the indices will be generated in sorted order.
Slice the input list using the indices start_idx and end_idx, and return the resulting sublist.
Test the function with some input lists.

Python3




import random
 
def randomRangeList(test_list):
    list_len = len(test_list)
    start_idx, end_idx = sorted(random.sample(range(list_len), 2))
    return str(test_list[start_idx:end_idx])
 
input_list1 = [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4]
print("\nThe original list is : ", input_list1)
print("Required List : " + randomRangeList(input_list1))
 
input_list2 = [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4]
print("\nThe original list is : ", input_list2)
print("Required List : " + randomRangeList(input_list2))
 
input_list3 = [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4]
print("\nThe original list is : ", input_list3)
print("Required List : " + randomRangeList(input_list3))


Output

The original list is :  [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4]
Required List : [7, 2]

The original list is :  [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4]
Required List : [4, 8, 10, 13, 5, 7, 2]

The original list is :  [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4]
Required List : [8, 10, 13, 5, 7]

Time complexity:  O(1).
Auxiliary space:  O(1) auxiliary space to store the two random indices and the length of the input list.

METHOD 3:Using loop 

APPROACH:

The program takes an original list as input and creates four different required lists based on the given criteria. The first required list contains elements 7 and 2 from the original list, the second required list contains elements between 4 and 14 from the original list, the third required list contains elements from index 3 to index 7 from the original list, and the fourth required list contains elements from index 3 to index 7 from the original list that are greater than 5.

ALGORITHM:

1.Create an original list of elements.
2.Create an empty list to hold the required elements.
3.For Required List 1, iterate over each element in the original list and check if it is equal to 7 or 2. If it is, append it to the required list.
4.For Required List 2, iterate over each element in the original list and check if it is greater than 3 and less than 14. If it is, append it to the required list.
5.For Required List 3, iterate over each index in the range of the length of the original list and check if it is between 3 and 7 (inclusive). If it is, append the element at that index to the required list.
6.For Required List 4, iterate over each index in the range of the length of the original list and check if it is between 3 and 7 (inclusive) and if the element at that index is greater than 5. If it is, append the element to the required list.
7.Print the required lists.

Python3




import random
 
# Original List
original_list = [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4]
 
# Required List 1
required_list_1 = []
for elem in original_list:
    if elem in [7, 2]:
        required_list_1.append(elem)
print("Required List 1: ", required_list_1)
 
# Required List 2
required_list_2 = []
for elem in original_list:
    if elem > 3 and elem < 14:
        required_list_2.append(elem)
print("Required List 2: ", required_list_2)
 
# Required List 3
required_list_3 = []
for i in range(len(original_list)):
    if i >= 3 and i <= 7:
        required_list_3.append(original_list[i])
print("Required List 3: ", required_list_3)


Output

Required List 1:  [7, 2]
Required List 2:  [4, 8, 10, 13, 5, 7, 4]
Required List 3:  [8, 10, 13, 5, 7]

Time complexity:
The time complexity of this program is O(n), where n is the length of the original list. This is because the program iterates over each element in the original list once for each required list.

Auxiliary Space:
The space complexity of this program is O(n), where n is the length of the original list. This is because the program creates a new list to hold each required list, and the length of each required list could be up to n.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads