0
set_a = {1, 2, 3, 4, 5, 6, 7, 8}

I want to have 4 packages with 2 numbers randomly taken from set_a. Condition is that if a number is chosen, it cannot be taken anymore.

a = [1, 2]
b = [3, 4]
c = [5, 6]
d = [7, 8]

I tried to achieve my goal with random.sample and list comparison but it didn't work out for me.

Next try was to remove the numbers from set with set_a.discard() and set_a.remove() which already have been chosen, but this didn't work either.

oOinsaneOo
  • 79
  • 5

1 Answers1

1

You can use this example how to shuffle the list and assign it to four variables:

from random import shuffle

set_a = {1, 2, 3, 4, 5, 6, 7, 8}

l = [*set_a]
shuffle(l)

a, b, c, d = zip(l[::2], l[1::2])
print(a, b, c, d, sep="\n")

Prints (for example):

(5, 1)
(7, 8)
(4, 2)
(6, 3)
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91