How to pick a random number not in a list in Python?


In this article, we will show you how to pick a random number not in a list in python. Below are the various methods to accomplish this task −

  • Using random.choice() function
  • Using random.choice() function and List Comprehension
  • Using random.choice() & set() functions
  • Using random.randrange() function

Using random.choice() function

The random.choice() method returns a random element from the specified sequence. The sequence could be a string, a range, a list, a tuple, or anything else.

Syntax

random.choice(sequence)

Parameters

sequence − any sequence like list, tuple etc.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword to import the random module.

  • Create a variable to store the input list and give some dummy values to it.

  • Use the max() function(returns the highest-valued item/greatest numbers in an iterable) to get the maximum element from the input list.

  • Create a new list(Empty list) to store the values that are not in the given list.

  • Loop from 0 to the max element using the for loop.

  • Inside the loop, check whether the iterator value is not present in the given list using the not in operator.

  • If it is not in the given list then add this element to the new list using the append() function.

  • select a random element from it using the random.choice() method(This function returns a random element from the specified sequence i.e list here).

  • Print the random element which is not in the list.

Example

The following program returns the random element which is not in the input list using random.choice() function −

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] print("Input List =",inputList) # getting the maximum/greatest element from the list max_element = max(inputList) # Taking an empty list to store elements that are not in the list newList = [] # Looping from 0 to max element using the for loop for i in range(max_element): # Checking whether the number is not in the given list if i not in inputList: # If it is not in the given list then add it to the newList newList.append(i) # Get the random number from the newlist using the choice() function of the random module randomElement = random.choice(newList) # printing the random element which is not in the list print("Random element which is not in the list = ", randomElement)

Output

On executing, the above program will generate the following output −

Input List = [3, 10, 5, 9, 2]
Random element which is not in the list = 8

Using random.choice() function and List Comprehension

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Loop from 0 to the max element and select elements that are not in list using the not in operator and select a random element from it using the random.choice() method.

  • Print the random element which is not in the list.

Example

The following program returns the random element which is not in the input list using random.choice() function and list comprehension −

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] print("Input List =",inputList) # getting the maximum/greatest element from the list max_element = max(inputList) # Get all the numbers from 0 to max_element that are not present in the list # Selecting a random element from those elements using the choice() function randomElement = random.choice([e for e in range(max_element) if e not in inputList]) # printing the random element which is not in the list print("The random element which is not in the list = ", randomElement)

Output

On executing, the above program will generate the following output −

Input List = [3, 10, 5, 9, 2]
The random element which is not in the list =  7

Using random.choice() & set() functions

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Using the set() function(creates a set object. A set list will appear in random order because the items are not ordered. It removes all the duplicates) and range() functions, obtain the set of numbers ranging from the lowest element of the input list to the highest element. Subtract this set from the set of the input list to remove the common elements, then convert it to a list and choose a random element from it using the random.choice() function.

  • Print the random element which is not in the list

Example

The following program returns a random element not in the given input list in python using the random.choice() and set() functions −

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] print("Input List = ",inputList) # Getting the set of numbers from min element of the input list to the maximum element using set() and range() functions # Subtracting this set from the set of the input list to remove the common elements # Converting this to a list and selecting a random element from this list randomElement = random.choice(list(set(range(min(inputList)+1, max(inputList)))-set(inputList))) # printing the random element which is not in the list print("Random element which is not in the list = ", randomElement)

Output

On executing, the above program will generate the following output −

Input List =  [3, 10, 5, 9, 2]
Random element which is not in the list =  4

Using random.randrange() function

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the while loop to run the loop infinite times by setting the condition as True.

  • Use the randrange() function(Returns a random number within the specified range)) to get the random element between 1 to 100.

  • Use the if conditional statement to check whether the generated random element is not present in the list using the not and in operators.

  • Use the break statement to break the loop if the condition is true(It means we found an element that is not in the given list).

  • Print the random element which is not in the list

Example

The following program returns a random element not in the given input list in python using the random.randrange() function–

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] print("Input List =", inputList) # Running the loop infinite times while True: # getting the random element from 1 to 100 randomElement=random.randrange(1, 100) # checking whether the generated random element is not present in the list if randomElement not in inputList: # breaking the loop if the condition is true break # printing the random element which is not in the list print("The random element which is not in the list = ", randomElement)

Output

On executing, the above program will generate the following output −

Input List = [3, 10, 5, 9, 2]
The random element which is not in the list =  96

Conclusion

In this article, we learned how to use Python's four different methods to select a random element that is not in the given list. We also learned how to remove duplicates using the set() function.

Updated on: 27-Oct-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements