0

I have a randoms list of names (37) and I need to pick 22 names out of it, given the condition that it can only be unique values and I need X number (22 this month) of names, the problem is that it does not gives me 22 names, it shows less. I tired this code:

random_auditores = []
for i in range(0,22):
    n = random.choice(auditores)
    if n not in random_auditores:
        random_auditores.append(n)
random_auditores
mjmoralesf
  • 15
  • 6
  • Short version: Replace all of that with just `random_auditores = random.sample(auditores, 22)`. Longer explanation: Your existing code fails because you don't `append` when you see a duplicate (if you changed the contents of the `for` to `while (n := random.choice(auditores)) in random.choice(auditores): pass`, followed by `random_auditores.append(n)` it would work, but for larger inputs where you want most of the values, it could waste a lot of time looping; `random.sample` changes algorithms to avoid pathological cases like that. – ShadowRanger Jun 27 '23 at 16:12
  • To be clear, nothing in your edit changed the fact that this is a duplicate, so I'm unclear on why you requested review. – ShadowRanger Jun 27 '23 at 16:14

0 Answers0