Python – Random range in list
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() + slicingimport random # function to Random range in listdef 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 listinput_list1 = [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4] # printing original listprint("\nThe original list is : ", input_list1) # printing resultprint("Required List : " + rabdomRangeList(input_list1)) # initializing listinput_list2 = [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4] # printing original listprint("\nThe original list is : ", input_list2) # printing resultprint("Required List : " + rabdomRangeList(input_list2)) # initializing listinput_list3 = [3, 19, 4, 8, 10, 13, 5, 7, 2, 1, 4] # printing original listprint("\nThe original list is : ", input_list3) # printing resultprint("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 : []


