# How can a single (or multiple) number(s) from a randomly generated list be used separately with no repeating values in either set? [Python]

## How can a single (or multiple) number(s) from a randomly generated list be used separately with no repeating values in either set? [Python]

Problem Description:

I am trying to separate a single number (or multiple) from a list of randomly generated list without any repeating values. This random list can be, for example, a range of 7 values from 1-45, then take one or more of those values from the list and use the same range but cannot be the same as the values inside the main list.

``````import random

rndList = []

rndList = random.sample(range(1,45), 7)
rndList.sort()
print("Some numbers...", rndList[0:6])
print("Separate number...", rndList[6])
``````

This will output a random and sorted list, e.g. `[5,12,28,35,39,41]` and the last separate value from the list, e.g. `[43]`. The problem is that the last number will always be greater than the previous whereas I need it to be between 1-45 but never use repeated values from the list.

Is it possible to select the separate value randomly from the list instead of creating another list? Appreciate the help!

## Solution – 1

The easiest thing would simply be to make your selection and remove it from the list before you sort the list.

``````rndList = random.sample(range(1,45), 7)
separate = rndList.pop()
rndList.sort()
print("Some numbers...", rndList)
print("Separate number...", separate)
``````
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.