Generate a List of Random Numbers Without Duplicates in Python Last Updated : 27 Nov, 2024 Comments Improve Suggest changes Like Article Like Report There are a few ways to generate a list of random numbers without duplicates in Python. Let’s look at how we can generate a list of random numbers without any duplicates.Using random.sample()This random.sample() method allows us to generate a list of unique random numbers in a single step. Python import random # Generate 5 unique random numbers between 1 and 10 a = random.sample(range(1, 11), 5) print(a) Output[8, 3, 6, 10, 5] Other methods which can help us to generate list of random numbers without duplicates in python are:Table of ContentUsing random.shuffle()Using numpy's random.choice() with replace=FalseUsing while LoopUsing random.shuffle()A more advanced method is to generate a list of number then shuffle it and then pick the first few numbers. random.shuffle() method works well when we want to avoid using loops or manual checks. Python import random a = list(range(1, 11)) # Create a list of numbers from 1 to 10 random.shuffle(a) # Shuffle the list a = a[:5] # Get the first 5 numbers from the shuffled list print(a) Output[7, 8, 9, 5, 6] Using numpy's random.choice() with replace=FalseIf we are working with large arrays or need more advanced random number generation, we can use numpy. The random.choice() function can be used to select unique values if replace=False. Python import numpy as np # Generate 10 unique random numbers from 1 to 100 a = np.random.choice(range(1, 101), size=10, replace=False) print(a) Output[ 5 92 42 76 91 22 75 69 63 33] Using while LoopAnother simple way to generate random numbers without duplicates is by using a while loop to keep adding random numbers to a list until we have enough unique numbers. We can check for duplicates by using a set. Python import random a = [] while len(a) < 5: num = random.randint(1, 10) # Check if the number is already in the list if num not in a: a.append(num) print(a) Output[1, 2, 4, 6, 5] Comment More infoAdvertise with us Next Article Generate a List of Random Numbers Without Duplicates in Python P pragya22r4 Follow Improve Article Tags : Python Python Programs Practice Tags : python Similar Reads Generate Random String Without Duplicates in Python When we need to create a random string in Python, sometimes we want to make sure that the string does not have any duplicate characters. For example, if we're generating a random password or a unique identifier, we might want to ensure each character appears only once. Using random.sample()Using ran 2 min read Generating random number list in Python In Python, we often need to generate a list of random numbers for tasks such as simulations, testing etc. Pythonâs random module provides multiple ways to generate random numbers. For example, we might want to create a list of 5 random numbers ranging from 1 to 100. This article explores different m 3 min read Python Generate Random Float Number Generating a random float number in Python means producing a decimal number that falls within a certain range, often between 0.0 and 1.0. Python provides multiple methods to generate random floats efficiently. Letâs explore some of the most effective ones.Using random.random()random.random() method 2 min read Python - Generate k random dates between two other dates Given two dates, the task is to write a Python program to get K dates randomly. Input : test_date1, test_date2 = date(2015, 6, 3), date(2015, 7, 1), K = 7 Output : [datetime.date(2015, 6, 18), datetime.date(2015, 6, 25), datetime.date(2015, 6, 29), datetime.date(2015, 6, 11), datetime.date(2015, 6, 4 min read How to Find Duplicates in a List - Python Finding duplicates in a list is a common task in programming. In Python, there are several ways to do this. Letâs explore the efficient methods to find duplicates. Using a Set (Most Efficient for Large Lists)Set() method is used to set a track seen elements and helps to identify duplicates. Pythona 2 min read Python - Generate Random String of given Length Generating random strings is a common requirement for tasks like creating unique identifiers, random passwords, or testing data. Python provides several efficient ways to generate random strings of a specified length. Below, weâll explore these methods, starting from the most efficient.Using random. 2 min read Python - Concatenate Random characters in String List Given a String list, perform concatenation of random characters. Input : test_list = ["Gfg", "is", "Best", "for", "Geeks"] Output : "GiBfe" Explanation : Random elements selected, e.g G from Gfg, etc.Input : test_list = ["Gfg", "is", "Best"] Output : "fst" Explanation : Random elements selected, e.g 6 min read Python - Remove Duplicates from a List Removing duplicates from a list is a common operation in Python which is useful in scenarios where unique elements are required. Python provides multiple methods to achieve this. Using set() method is most efficient for unordered lists. Converting the list to a set removes all duplicates since sets 2 min read Counting number of unique values in a Python list Counting the number of unique values in a Python list involves determining how many distinct elements are present disregarding duplicates.Using a SetUsing a set to count the number of unique values in a Python list leverages the property of sets where each element is stored only once.Pythonli = [1, 2 min read Python | Sort given list by frequency and remove duplicates Problems associated with sorting and removal of duplicates is quite common in development domain and general coding as well. The sorting by frequency has been discussed, but sometimes, we even wish to remove the duplicates without using more LOC's and in a shorter way. Let's discuss certain ways in 5 min read Like