Question: I'm needing help on a small logic using python.

I have several lists of different sizes. For each list I need to generate 5 different sets (lists) and each set formed must have 5 elements. Each element of the set must be generated randomly.

It is important to note that a set {a, b, c} is equal to the set {b, a, c}.

```
Example: L1 = {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s}

Set 1 = {a, c, e, h, i, s}
Set 2 = {a, b, c, d, e, f}
Set 3 = {m, n, o, p, q, r}
Set 4 = {e, n, k, a, i, s}
Set 5 = {h, ​​p, a, i, g, k}

```

Could anyone help me in Python logic? Thank you!

Answer: 
Combine [`random.sample`](https://docs.python.org/library/random.html#random.sample) with [`itertools.combinations`](https://docs.python.org/library/itertools.html#itertools.combinations):

```
>>> import itertools
>>> import random

>>> [set(i) for i in random.sample(list(itertools.combinations(L1, 5)), 5)]
[{'c', 'h', 'i', 'l', 'q'},
 {'a', 'd', 'o', 'p', 'r'},
 {'b', 'h', 'j', 'k', 'l'},
 {'b', 'd', 'g', 'n', 's'},
 {'d', 'f', 'i', 'l', 's'}]

```

Or if the `combinations` is too slow then you could take samples as long as you don't have 5 different ones:

```
res = []
while len(res) < 5:
    samp = set(random.sample(L1, 5))
    if samp not in res:
        res.append(samp)

res
[{'a', 'b', 'd', 'g', 'q'},
 {'e', 'g', 'i', 'j', 'q'},
 {'h', 'i', 'j', 'k', 'n'},
 {'b', 'e', 'j', 'n', 's'},
 {'e', 'f', 'g', 'l', 'r'}]

```
You can use a hit-and-miss approach if you want to generate 5 distinct sets:

```
import random

L1 = {c for c in 'abcdefghijklmnopqrstuvwxyz'}
sets = set()
items = list(L1)
while len(sets) < 5:
    s = frozenset(random.sample(items,5))
    sets.add(s)

sets = [set(s) for s in sets]

for s in sets: print(s)

```

Typical output:

```
{'w', 'b', 'p', 'j', 'r'}
{'b', 'p', 'v', 'l', 'o'}
{'q', 'c', 'i', 'z', 't'}
{'j', 'f', 'h', 'l', 'y'}
{'b', 'h', 'd', 'r', 'c'}

```

For problems of the given size, this should be adequate. 26 choose 5 = 65780, so collisions should be rare. As `L1` gets closer to 5 in size it will become more difficult to get 5 distinct sets.