Python - Random Range in List Last Updated : 13 Feb, 2025 Comments Improve Suggest changes Like Article Like Report We are given a list we need to find random range in list. For example, a = [10, 20, 30, 40, 50, 60] we need to find random ranges so that given output should be random list and it changes in every execution.Using random.samplerandom.sample can randomly pick elements from the list and preserve original order. Python import random a = [10, 20, 30, 40, 50, 60] # Create a shuffled version of the list s = a[:] random.shuffle(s) # Take a random slice from the shuffled list size = random.randint(1, len(s)) r = s[:size] print(r) Output[20, 30, 10, 40, 60, 50] Explanation:A copy of list a is shuffled to avoid modifying the original.A random-length slice is taken from shuffled list and printed.Using random.randintCode creates a shuffled version of the list a to randomize the order of its elements without modifying original list. Using random.randint, it generates a random length between 1 and size of shuffled list and slices list up to this length. This produces a random sublist which is then printed. Python import random a = [10, 20, 30, 40, 50, 60] # Generate random start and end indices s = random.randint(0, len(a) - 1) e = random.randint(s, len(a)) # Create a random sublist using slicing r = a[s:e] print(r) Output[20, 30, 40] Explanation:Code generates a random start index s and a random end index e (ensuring e >= s) to define a random range within the list.A slice is taken from original list a using indices s and e producing a random sublist r which is then printed.Using random.samplerandom.sample selects a specified number of unique elements randomly from the list without repetition. It can be used to generate random sublists of any size up to list's length. Python import random a = [10, 20, 30, 40, 50, 60] # Randomly choose the size of the sublist s = random.randint(1, len(a)) # Extract a random sublist while preserving the original order r = sorted(random.sample(a, s), key=a.index) print(r) Output[20, 30] Explanation:Code uses random.sample to randomly select s unique elements from the list a, with s being a random size between 1 and the length of list.Selected elements are sorted by their original order using key=a.index to maintain sequence as it appeared in original list. Comment More infoAdvertise with us Next Article Python - Random Range in List M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Practice Tags : python Similar Reads Python - Non-overlapping Random Ranges Sometimes, while working with Python, we can have problem in which we need to extract N random ranges which are non-overlapping in nature and with given range size. This can have applications in which we work with data. Lets discuss certain way in which this task can be performed. Method : Using any 4 min read Python - Get Random Range Average Given range and Size of elements, extract random numbers within a range, and perform average of it. Input : N = 3, strt_num = 10, end_num = 15 Output : 13.58 Explanation : Random elements extracted between 10 and 15, averaging out to 13.58. Input : N = 2, strt_num = 13, end_num = 18 Output : 15.82 E 5 min read Python - N Random Tuples list Sometimes, while working with Python records, we can have a problem in which we need to construct a random tuple list. This can have applications in many domains using gaming and day-to-day programming. Let's discuss specific ways in which this task can be performed in Python. Input: N = 4 R = 6 Out 3 min read Python | Remove random element from list Sometimes, while working with Python lists, we can have a problem or part of it, in which we desire to convert a list after deletion of some random element. This can have it's application in gaming domain or personal projects. Let's discuss certain way in which this task can be done. Method : Using 3 min read Python - Random Numbers Summation We need to do summation of random numbers. For example, n = [random.randint(1, 10) for _ in range(5)] it will generate random numbers [4, 8, 9, 4, 6] between 1 to 10 so the resultant output should be 29.Using random.randint()random.randint(a, b) function generates a random integer between a and b (i 2 min read Like