Randomly select elements from list without repetition in Python Last Updated : 15 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report We are given a list, and our task is to randomly select elements from it without repetition. This means that once an element is selected, it should not be picked again. For example, given a = [10, 20, 30, 40, 50], selecting three elements randomly could result in [30, 10, 50], but the same element should not appear twice. Let's discuss different ways to do this in Python.Using random.sample()random.sample() function selects unique elements from a list in a single step without repetition. Python import random a = [10, 20, 30, 40, 50] # Selecting 3 unique elements res = random.sample(a, 3) print(res) Output[50, 30, 10] Explanation:random.sample(a, 3) selects 3 unique elements from a.The function ensures no duplicates and maintains the order of selection.This method is efficient because it handles selection in a single operation.Let's explore some more ways and see how we can randonly select elements from list without repetition in python.Table of ContentUsing random.shuffle() and SlicingUsing random.choice() Using set() Using random.shuffle() and Slicingrandom.shuffle() function randomly rearranges the list, and we can take the first n elements as the selection. Python import random a = [10, 20, 30, 40, 50] # Shuffling the list random.shuffle(a) # Selecting first 3 elements res = a[:3] print(res) Output[20, 40, 30] Explanation:random.shuffle(a) rearranges elements in place.Slicing a[:3] takes the first 3 elements from the shuffled list.This method is useful when order randomness matters.Using random.choice() random.choice() function picks a random element, and we can remove selected elements to avoid repetition. Python import random a = [10, 20, 30, 40, 50] # Selecting 3 unique elements res = [] for _ in range(3): choice = random.choice(a) # Pick a random element res.append(choice) a.remove(choice) # Remove chosen element to prevent repetition print(res) Output[20, 10, 30] Explanation:random.choice(a) selects a random element.a.remove(choice) removes the selected element to avoid repetition.This method modifies the original list and is less efficient for large lists.Using set() We can use a set to keep track of selected elements and avoid modifying the original list. Python import random a = [10, 20, 30, 40, 50] # Selecting 3 unique elements selected = set() while len(selected) < 3: selected.add(random.choice(a)) res = list(selected) print(res) Output[40, 10, 20] Explanation:random.choice(a) picks a random element.selected.add(choice) ensures no duplicates.The loop continues until 3 unique elements are selected. Comment More infoAdvertise with us Next Article How to randomly select elements of an array with NumPy in Python ? R riturajsaha Follow Improve Article Tags : Python python-list Python list-programs Python-random Practice Tags : pythonpython-list Similar Reads Randomly Select N Elements from List in Python When working with lists in Python, we often need to randomly select a specific number of elements. For example, consider the list a = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]. We might want to randomly select 3 elements from this list. Let's discuss different ways for selecting n random elements fr 3 min read Select Random Element from Set in Python Selecting a random element from a group of samples is a deterministic task. A computer program can mimic such a simulation of random choices by using a pseudo-random generator. In this article, we will learn how to select a random element from a set in Python. What is a Set in Python?A Set is an uno 3 min read How to Select a Random Element from a Tuple in Python Selecting a random element consists of retrieving one element from a collection in an unpredictable manner. In Python, this can be done easily using different methods provided by the random module. Below are the three different approaches to select a random element from a tuple. Select a Random Elem 2 min read How to randomly select elements of an array with NumPy in Python ? Randomly selecting elements from an array means choosing random elements from the array. NumPy offers several efficient methods to pick elements either with or without repetition. For example, if you have an array [1, 2, 3, 4, 5] and want to randomly select 3 unique elements, the output might look l 2 min read How to randomly select elements of an array with NumPy in Python ? Randomly selecting elements from an array means choosing random elements from the array. NumPy offers several efficient methods to pick elements either with or without repetition. For example, if you have an array [1, 2, 3, 4, 5] and want to randomly select 3 unique elements, the output might look l 2 min read Select random value from a list-Python The goal here is to randomly select a value from a list in Python. For example, given a list [1, 4, 5, 2, 7], we want to retrieve a single randomly chosen element, such as 5. There are several ways to achieve this, each varying in terms of simplicity, efficiency and use case. Let's explore different 2 min read Like