 Open in App
Not now

# Python | Generate random number except K in list

• Last Updated : 05 Feb, 2023

Sometimes, while working with python, we can have a problem in which we need to generate random number. This seems quite easy but sometimes we require a slight variation of it. That is, we require to generate random number from a list except K. Lets discuss certain ways in which this task can be performed.

Method #1 : Using choice() + list comprehension The combination of above functions can be used to perform this task. In this, we first filter out the numbers except K using list comprehension and then feed that list to choice() for random number generation.

## Python3

 `# Python3 code to demonstrate``# Generate random number except K in list``# using choice() + list comprehension``import` `random` `# Initializing list``test_list ``=` `[``4``, ``7``, ``8``, ``4``, ``6``, ``10``]` `# printing original list``print``("The original ``list` `is` `: " ``+` `str``(test_list))` `# Initializing K``K ``=` `4` `# Generate random number except K in list``# using choice() + list comprehension``res ``=` `random.choice([ele ``for` `ele ``in` `test_list ``if` `ele !``=` `K])` `# printing result``print` `("The random number ``except` `K ``is` `: " ``+` `str``(res))`

Output :

```The original list is : [4, 7, 8, 4, 6, 10]
The random number except K is : 8```

Method #2 : Using filter() + lambda + choice() This is yet another way in which this task can be performed. In this, we perform method of creating new list using filter and lambda.

## Python3

 `# Python3 code to demonstrate``# Generate random number except K in list``# using choice() + filter() + lambda``import` `random` `# Initializing list``test_list ``=` `[``4``, ``7``, ``8``, ``4``, ``6``, ``10``]` `# printing original list``print``("The original ``list` `is` `: " ``+` `str``(test_list))` `# Initializing K``K ``=` `4` `# Generate random number except K in list``# using choice() + filter() + lambda``res ``=` `random.choice(``list``(``filter``(``lambda` `ele: ele !``=` `K, test_list)))` `# printing result``print` `("The random number ``except` `K ``is` `: " ``+` `str``(res))`

Output :

```The original list is : [4, 7, 8, 4, 6, 10]
The random number except K is : 8```

Method 3: Using random.sample()
This method uses the random.sample() function from the random module to select a random element from the list without replacement, which means that the selected element will not be present in the list after being selected.

## Python3

 `#Python3 code to demonstrate``#Generate random number except K in list``#using random.sample()``import` `random` `#Initializing list``test_list ``=` `[``4``, ``7``, ``8``, ``4``, ``6``, ``10``]` `#printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `#Initializing K``K ``=` `4` `#Generate random number except K in list``#using random.sample()``res ``=` `random.sample([ele ``for` `ele ``in` `test_list ``if` `ele !``=` `K], ``1``)[``0``]` `#printing result``print` `(``"The random number except K is : "` `+` `str``(res))`

Output

```The original list is : [4, 7, 8, 4, 6, 10]
The random number except K is : 7```

Time complexity: O(n) where n is the length of the list
Auxiliary Space: O(n)

Explanation:
The code filters out all elements equal to K using a list comprehension and then selects one random element from the filtered list using random.sample(). The selected element is then accessed using  indexing.

My Personal Notes arrow_drop_up