[Tutor] Creating lists with definite (n) items without repetitions

Danny Yoo dyoo at hashcollision.org
Thu Sep 3 21:44:29 CEST 2015


> At first I thought you might want itertools.combinations()
>
>>>> import string, itertools
>>>> for t in itertools.combinations(string.ascii_lowercase, 3):
> ...     print t # list(t) if you actually need a list
> ...
> ('a', 'b', 'c')
> ('a', 'b', 'd')
> ('a', 'b', 'e')
> ('a', 'b', 'f')
> ('a', 'b', 'g')
> [snip]
>
> but that gives
>
>>>> sum(1 for t in itertools.combinations(string.ascii_lowercase, 3))
> 2600

At the very least, that matches the expected result mathematically,
since the number of combinations of 3 elements out of 26 possibilities
is "26 choose 3".

    http://mathworld.wolfram.com/Combination.html

and "26 choose 3" computes to 2600.

#######################
>>> 26 * 25 * 24 / (3 * 2 * 1)
2600
#######################


More information about the Tutor mailing list