Creating combination on list values - python

I have a requirement where say i have a list
lis = ['a','b','c','d','e','f']
I have to now create a combination of them eg:
l1 = [a],['b,c,d,e,f]
l2: [b],[a,c,d,e,f]
.
.
l10 [a,b,c],[d,e,f]
.
l11 [a,b,c,d] [e,f]
The repeated elements on the left and right nodes will be removed:
eg: i don't need two lists as:
l1: [b,c] , [a,d,e,f]
l2: [a,d,e,f], [b,c]
Since they are the same
The pseudo code i have in mind is:
for length = 1, i will take one element from list and club others
similar for length=2, will take two element and club others
till length=len(list)-1, will do the clubbing
and then later remove the duplicates.
Any better solution i could try?

This may no be optimal, but is very straightforward:
from itertools import chain, combinations
def power_set(iterable):
"""power_set([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"""
source = list(iterable)
return chain.from_iterable(combinations(source, r) for r in range(1, len(source) // 2 + 1))
def complement(source, universe):
return tuple(set(universe) - set(source))
lst = ['a', 'b', 'c', 'd', 'e', 'f']
result = set(frozenset({si, complement(si, lst)}) for si in power_set(lst))
for s, c in result:
print(s, c)
Output
('a', 'd', 'e') ('f', 'c', 'b')
('f', 'a', 'c', 'b') ('d', 'e')
('b', 'e') ('d', 'f', 'a', 'c')
('a', 'b', 'f') ('d', 'e', 'c')
('e', 'd', 'a', 'f', 'b') ('c',)
('c', 'f') ('d', 'a', 'e', 'b')
('d', 'f') ('a', 'e', 'b', 'c')
('d',) ('e', 'c', 'a', 'f', 'b')
('f', 'a', 'e', 'c') ('b', 'd')
('e', 'c', 'd', 'a', 'b') ('f',)
('b', 'c', 'd') ('f', 'a', 'e')
('a', 'b', 'e') ('d', 'f', 'c')
('b', 'c') ('d', 'f', 'a', 'e')
('f', 'a', 'b') ('c', 'd', 'e')
('d', 'e', 'b', 'c') ('a', 'f')
('c', 'd', 'f') ('a', 'e', 'b')
('e', 'c', 'd', 'f', 'b') ('a',)
('a', 'c') ('d', 'f', 'e', 'b')
('f', 'e', 'c') ('a', 'b', 'd')
('a', 'd') ('f', 'e', 'b', 'c')
('b', 'c', 'e') ('d', 'f', 'a')
('a', 'c', 'e') ('d', 'f', 'b')
('d', 'e', 'f') ('a', 'c', 'b')
('a', 'c', 'd') ('f', 'e', 'b')
('d', 'f', 'e', 'c') ('a', 'b')
('f', 'a', 'e', 'b') ('c', 'd')
('d', 'a', 'c') ('b', 'e', 'f')
('a', 'e') ('d', 'f', 'c', 'b')
('a', 'b', 'c') ('d', 'f', 'e')
('a', 'd', 'f') ('e', 'b', 'c')
('d', 'e', 'b') ('a', 'c', 'f')
('c', 'd', 'a', 'f', 'b') ('e',)
('b',) ('e', 'c', 'd', 'a', 'f')
('e', 'f') ('d', 'a', 'c', 'b')
('d', 'c', 'b') ('a', 'e', 'f')
('b', 'f') ('d', 'a', 'e', 'c')
('d', 'a', 'e') ('b', 'c', 'f')
('b', 'd', 'e') ('f', 'a', 'c')
('a', 'e', 'c') ('b', 'd', 'f')
('c', 'e') ('d', 'f', 'a', 'b')
('d', 'a', 'b') ('c', 'e', 'f')

Related

List every combination of a list's elements

I'm trying to retrieve every combination of a list's elements in a variable but one at a time,
I used this code that I modified to fit my program :
import itertools
stuff = [1, 2, 3]
for L in range(0, len(stuff)+1):
for subset in itertools.combinations(stuff, L):
print(subset)
in my program, the list "stuff" contains the alphabet and numbers,
but the problem is that I retrieve the combinations like "a", "b",..., "y", "z" and then i would like to get "aa", "ab", "ac",... but it doesn't return "aa", as if it skipped it, it comes to "ab" directly.
I'm new to this forum, it's my first question so it might not be perfect
Thanks for any help :)
You're not looking for combinations of elements from an existing collection, but for all possible products of the elements of that collection:
import itertools
stuff = ['a', 'b', 'c']
for n in range(0, len(stuff) + 1):
for result in itertools.product(stuff, repeat=n):
print(result)
Output:
()
('a',)
('b',)
('c',)
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'b')
('b', 'c')
('c', 'a')
('c', 'b')
('c', 'c')
('a', 'a', 'a')
('a', 'a', 'b')
('a', 'a', 'c')
('a', 'b', 'a')
('a', 'b', 'b')
('a', 'b', 'c')
('a', 'c', 'a')
('a', 'c', 'b')
('a', 'c', 'c')
('b', 'a', 'a')
('b', 'a', 'b')
('b', 'a', 'c')
('b', 'b', 'a')
('b', 'b', 'b')
('b', 'b', 'c')
('b', 'c', 'a')
('b', 'c', 'b')
('b', 'c', 'c')
('c', 'a', 'a')
('c', 'a', 'b')
('c', 'a', 'c')
('c', 'b', 'a')
('c', 'b', 'b')
('c', 'b', 'c')
('c', 'c', 'a')
('c', 'c', 'b')
('c', 'c', 'c')
Note: I renamed L to n, because it's generally not a good idea to name variables and functions in Python with capitals and I renamed subset to result because what both combinations and product produce are not sets but tuples.
As #mozway correctly points out, you may consider an example like ('a', 'b') and ('b', 'a') duplicates, in which case you want combinations_with_replacement:
import itertools
stuff = ['a', 'b', 'c']
for n in range(0, len(stuff) + 1):
for result in itertools.combinations_with_replacement(stuff, r=n):
print(result)
Output:
()
('a',)
('b',)
('c',)
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'b')
('b', 'c')
('c', 'c')
('a', 'a', 'a')
('a', 'a', 'b')
('a', 'a', 'c')
('a', 'b', 'b')
('a', 'b', 'c')
('a', 'c', 'c')
('b', 'b', 'b')
('b', 'b', 'c')
('b', 'c', 'c')
('c', 'c', 'c')

Filter List of Tuples to Exclude from Another List of Tuples which Contains

(Using Python3)
I have a list of tuples, (of strings)
have = [
('a', 'b', 'c', 'd'), ('a', 'b', 'c', 'e'), ('a', 'b', 'c', 'f'), ('a', 'b', 'c', 'g'), ('a', 'b', 'd', 'e'),
('a', 'b', 'd', 'f'), ('a', 'b', 'd', 'g'), ('a', 'b', 'e', 'f'), ('a', 'b', 'e', 'g'), ('a', 'b', 'f', 'g'),
('a', 'c', 'd', 'e'), ('a', 'c', 'd', 'f'), ('a', 'c', 'd', 'g'), ('a', 'c', 'e', 'f'), ('a', 'c', 'e', 'g'),
('a', 'c', 'f', 'g'), ('a', 'd', 'e', 'f'), ('a', 'd', 'e', 'g'), ('a', 'd', 'f', 'g'), ('a', 'e', 'f', 'g'),
('b', 'c', 'd', 'e'), ('b', 'c', 'd', 'f'), ('b', 'c', 'd', 'g'), ('b', 'c', 'e', 'f'), ('b', 'c', 'e', 'g'),
('b', 'c', 'f', 'g'), ('b', 'd', 'e', 'f'), ('b', 'd', 'e', 'g'), ('b', 'd', 'f', 'g'), ('b', 'e', 'f', 'g'),
('c', 'd', 'e', 'f'), ('c', 'd', 'e', 'g'), ('c', 'd', 'f', 'g'), ('c', 'e', 'f', 'g'), ('d', 'e', 'f', 'g')
]
I also have a list of tuples (also strings) which I want to "exclude"
exclude = [('a', 'd'), ('b', 'c')]
I'm trying to find an efficient way to remove any element in have that contains both the elements in each exclude tuple. Ordering does not matter.
My goal is to return something like this:
[
('a', 'b', 'e', 'f'), ('a', 'b', 'e', 'g'), ('a', 'b', 'f', 'g'), ('a', 'c', 'e', 'f'), ('a', 'c', 'e', 'g'),
('a', 'c', 'f', 'g'), ('a', 'e', 'f', 'g'), ('b', 'd', 'e', 'f'), ('b', 'd', 'e', 'g'), ('b', 'd', 'f', 'g'),
('b', 'e', 'f', 'g'), ('c', 'd', 'e', 'f'), ('c', 'd', 'e', 'g'), ('c', 'd', 'f', 'g'), ('c', 'e', 'f', 'g'),
('d', 'e', 'f', 'g')
]
You could convert the exclude tuples to sets and then check for each element of have is the excluded set isn't a subset of it:
excludeSet = [set(e) for e in exclude]
filteredHave = [h for h in have if not any(e for e in excludeSet if e.issubset(h))]

Python itertools generating a powerset with replacements

I'm trying to generate a powerset of a large list of strings that includes replacements. None of the built-in itertools functions do this (as far as I can tell).
EDIT Maybe using "set" in the title was misleading. Order is important here. 'ca' != 'ac'
Example of what I want:
print( powerset_with_replacement(['abc']) )
# yeilds
('a',)
('b',)
('c',)
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'b')
('b', 'c')
('c', 'c')
('a', 'a', 'a')
('a', 'a', 'b')
('a', 'a', 'c')
('a', 'b', 'b')
('a', 'b', 'c')
('a', 'c', 'c')
('b', 'b', 'b')
('b', 'b', 'c')
('b', 'c', 'c')
('c', 'c', 'c')
I got this far already but it seems messy.. I'm wondering if there is a cleaner or more pythonic way to do this. Any help is much appreciated.
Is there a way to get rid of the flatten function or put that inside the main one?
from itertools import combinations_with_replacement
import collections
def flatten(it):
for x in it:
if (isinstance(x, collections.abc.Iterable) and
not isinstance(x, tuple)):
yield from flatten(x)
else:
yield x
def powerset_with_replacement(list1):
for i in range(1,len(list1)+1):
yield( combinations_with_replacement(list1, i) )
y = flatten(powerset_with_replacement(list('abc')))
for x in y:
print(x)
You can define your own generator and mix in itertools.product to achieve this result:
def combinations_upto(x):
for i in range(len(x)):
yield from itertools.product(x, repeat=i+1)
x = "abc"
combo_list = list(combinations_upto(x))
print(combo_list)
Create a list of cartesian products of size 1 to len(X)
from itertools import product, chain
x = 'abc'
prods = chain.from_iterable(product(x,repeat=n) for n in range(1,len(x)+1))
[print(_) for _ in prods]
Output
('a',)
('b',)
('c',)
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'b')
('b', 'c')
('c', 'a')
('c', 'b')
('c', 'c')
('a', 'a', 'a')
('a', 'a', 'b')
('a', 'a', 'c')
('a', 'b', 'a')
('a', 'b', 'b')
('a', 'b', 'c')
('a', 'c', 'a')
('a', 'c', 'b')
('a', 'c', 'c')
('b', 'a', 'a')
('b', 'a', 'b')
('b', 'a', 'c')
('b', 'b', 'a')
('b', 'b', 'b')
('b', 'b', 'c')
('b', 'c', 'a')
('b', 'c', 'b')
('b', 'c', 'c')
('c', 'a', 'a')
('c', 'a', 'b')
('c', 'a', 'c')
('c', 'b', 'a')
('c', 'b', 'b')
('c', 'b', 'c')
('c', 'c', 'a')
('c', 'c', 'b')
('c', 'c', 'c')

return all possible combos of DNA

My code below gives me all possible combinations of DNA. Is there a more efficient, cleaner way to do this? Also, for any bioinformatics or biotech programmers, which modules should I become most familiar with?
DNA = 'a', 't', 'g', 'c'
lis = []
def all_combos():
for a in A:
for t in A:
for g in A:
for c in A:
lis.append([a, t, g, c])
return lis
print(all_combos())
You can use itertools.product to generate the list of all combinations. This will generate tuples instead of lists, but I guess that's fine?
from itertools import product
lis = list(product('atgc',repeat=4))
Here 4 means you want to construct 4-tuples.
The algorithm is of course not faster - complexity-wise - than using the for loops, since it is inherently O(mn) with m the number of elements (len('atgc')) and n=4 (the number of elements per tuple). Both algorithms are in terms of big-oh equally fast (although there can be differences).
This yields:
>>> list(product('atgc',repeat=4))
[('a', 'a', 'a', 'a'), ('a', 'a', 'a', 't'), ('a', 'a', 'a', 'g'), ('a', 'a', 'a', 'c'), ('a', 'a', 't', 'a'), ('a', 'a', 't', 't'), ('a', 'a', 't', 'g'), ('a', 'a', 't', 'c'), ('a', 'a', 'g', 'a'), ('a', 'a', 'g', 't'), ('a', 'a', 'g', 'g'), ('a', 'a', 'g', 'c'), ('a', 'a', 'c', 'a'), ('a', 'a', 'c', 't'), ('a', 'a', 'c', 'g'), ('a', 'a', 'c', 'c'), ('a', 't', 'a', 'a'), ('a', 't', 'a', 't'), ('a', 't', 'a', 'g'), ('a', 't', 'a', 'c'), ('a', 't', 't', 'a'), ('a', 't', 't', 't'), ('a', 't', 't', 'g'), ('a', 't', 't', 'c'), ('a', 't', 'g', 'a'), ('a', 't', 'g', 't'), ('a', 't', 'g', 'g'), ('a', 't', 'g', 'c'), ('a', 't', 'c', 'a'), ('a', 't', 'c', 't'), ('a', 't', 'c', 'g'), ('a', 't', 'c', 'c'), ('a', 'g', 'a', 'a'), ('a', 'g', 'a', 't'), ('a', 'g', 'a', 'g'), ('a', 'g', 'a', 'c'), ('a', 'g', 't', 'a'), ('a', 'g', 't', 't'), ('a', 'g', 't', 'g'), ('a', 'g', 't', 'c'), ('a', 'g', 'g', 'a'), ('a', 'g', 'g', 't'), ('a', 'g', 'g', 'g'), ('a', 'g', 'g', 'c'), ('a', 'g', 'c', 'a'), ('a', 'g', 'c', 't'), ('a', 'g', 'c', 'g'), ('a', 'g', 'c', 'c'), ('a', 'c', 'a', 'a'), ('a', 'c', 'a', 't'), ('a', 'c', 'a', 'g'), ('a', 'c', 'a', 'c'), ('a', 'c', 't', 'a'), ('a', 'c', 't', 't'), ('a', 'c', 't', 'g'), ('a', 'c', 't', 'c'), ('a', 'c', 'g', 'a'), ('a', 'c', 'g', 't'), ('a', 'c', 'g', 'g'), ('a', 'c', 'g', 'c'), ('a', 'c', 'c', 'a'), ('a', 'c', 'c', 't'), ('a', 'c', 'c', 'g'), ('a', 'c', 'c', 'c'), ('t', 'a', 'a', 'a'), ('t', 'a', 'a', 't'), ('t', 'a', 'a', 'g'), ('t', 'a', 'a', 'c'), ('t', 'a', 't', 'a'), ('t', 'a', 't', 't'), ('t', 'a', 't', 'g'), ('t', 'a', 't', 'c'), ('t', 'a', 'g', 'a'), ('t', 'a', 'g', 't'), ('t', 'a', 'g', 'g'), ('t', 'a', 'g', 'c'), ('t', 'a', 'c', 'a'), ('t', 'a', 'c', 't'), ('t', 'a', 'c', 'g'), ('t', 'a', 'c', 'c'), ('t', 't', 'a', 'a'), ('t', 't', 'a', 't'), ('t', 't', 'a', 'g'), ('t', 't', 'a', 'c'), ('t', 't', 't', 'a'), ('t', 't', 't', 't'), ('t', 't', 't', 'g'), ('t', 't', 't', 'c'), ('t', 't', 'g', 'a'), ('t', 't', 'g', 't'), ('t', 't', 'g', 'g'), ('t', 't', 'g', 'c'), ('t', 't', 'c', 'a'), ('t', 't', 'c', 't'), ('t', 't', 'c', 'g'), ('t', 't', 'c', 'c'), ('t', 'g', 'a', 'a'), ('t', 'g', 'a', 't'), ('t', 'g', 'a', 'g'), ('t', 'g', 'a', 'c'), ('t', 'g', 't', 'a'), ('t', 'g', 't', 't'), ('t', 'g', 't', 'g'), ('t', 'g', 't', 'c'), ('t', 'g', 'g', 'a'), ('t', 'g', 'g', 't'), ('t', 'g', 'g', 'g'), ('t', 'g', 'g', 'c'), ('t', 'g', 'c', 'a'), ('t', 'g', 'c', 't'), ('t', 'g', 'c', 'g'), ('t', 'g', 'c', 'c'), ('t', 'c', 'a', 'a'), ('t', 'c', 'a', 't'), ('t', 'c', 'a', 'g'), ('t', 'c', 'a', 'c'), ('t', 'c', 't', 'a'), ('t', 'c', 't', 't'), ('t', 'c', 't', 'g'), ('t', 'c', 't', 'c'), ('t', 'c', 'g', 'a'), ('t', 'c', 'g', 't'), ('t', 'c', 'g', 'g'), ('t', 'c', 'g', 'c'), ('t', 'c', 'c', 'a'), ('t', 'c', 'c', 't'), ('t', 'c', 'c', 'g'), ('t', 'c', 'c', 'c'), ('g', 'a', 'a', 'a'), ('g', 'a', 'a', 't'), ('g', 'a', 'a', 'g'), ('g', 'a', 'a', 'c'), ('g', 'a', 't', 'a'), ('g', 'a', 't', 't'), ('g', 'a', 't', 'g'), ('g', 'a', 't', 'c'), ('g', 'a', 'g', 'a'), ('g', 'a', 'g', 't'), ('g', 'a', 'g', 'g'), ('g', 'a', 'g', 'c'), ('g', 'a', 'c', 'a'), ('g', 'a', 'c', 't'), ('g', 'a', 'c', 'g'), ('g', 'a', 'c', 'c'), ('g', 't', 'a', 'a'), ('g', 't', 'a', 't'), ('g', 't', 'a', 'g'), ('g', 't', 'a', 'c'), ('g', 't', 't', 'a'), ('g', 't', 't', 't'), ('g', 't', 't', 'g'), ('g', 't', 't', 'c'), ('g', 't', 'g', 'a'), ('g', 't', 'g', 't'), ('g', 't', 'g', 'g'), ('g', 't', 'g', 'c'), ('g', 't', 'c', 'a'), ('g', 't', 'c', 't'), ('g', 't', 'c', 'g'), ('g', 't', 'c', 'c'), ('g', 'g', 'a', 'a'), ('g', 'g', 'a', 't'), ('g', 'g', 'a', 'g'), ('g', 'g', 'a', 'c'), ('g', 'g', 't', 'a'), ('g', 'g', 't', 't'), ('g', 'g', 't', 'g'), ('g', 'g', 't', 'c'), ('g', 'g', 'g', 'a'), ('g', 'g', 'g', 't'), ('g', 'g', 'g', 'g'), ('g', 'g', 'g', 'c'), ('g', 'g', 'c', 'a'), ('g', 'g', 'c', 't'), ('g', 'g', 'c', 'g'), ('g', 'g', 'c', 'c'), ('g', 'c', 'a', 'a'), ('g', 'c', 'a', 't'), ('g', 'c', 'a', 'g'), ('g', 'c', 'a', 'c'), ('g', 'c', 't', 'a'), ('g', 'c', 't', 't'), ('g', 'c', 't', 'g'), ('g', 'c', 't', 'c'), ('g', 'c', 'g', 'a'), ('g', 'c', 'g', 't'), ('g', 'c', 'g', 'g'), ('g', 'c', 'g', 'c'), ('g', 'c', 'c', 'a'), ('g', 'c', 'c', 't'), ('g', 'c', 'c', 'g'), ('g', 'c', 'c', 'c'), ('c', 'a', 'a', 'a'), ('c', 'a', 'a', 't'), ('c', 'a', 'a', 'g'), ('c', 'a', 'a', 'c'), ('c', 'a', 't', 'a'), ('c', 'a', 't', 't'), ('c', 'a', 't', 'g'), ('c', 'a', 't', 'c'), ('c', 'a', 'g', 'a'), ('c', 'a', 'g', 't'), ('c', 'a', 'g', 'g'), ('c', 'a', 'g', 'c'), ('c', 'a', 'c', 'a'), ('c', 'a', 'c', 't'), ('c', 'a', 'c', 'g'), ('c', 'a', 'c', 'c'), ('c', 't', 'a', 'a'), ('c', 't', 'a', 't'), ('c', 't', 'a', 'g'), ('c', 't', 'a', 'c'), ('c', 't', 't', 'a'), ('c', 't', 't', 't'), ('c', 't', 't', 'g'), ('c', 't', 't', 'c'), ('c', 't', 'g', 'a'), ('c', 't', 'g', 't'), ('c', 't', 'g', 'g'), ('c', 't', 'g', 'c'), ('c', 't', 'c', 'a'), ('c', 't', 'c', 't'), ('c', 't', 'c', 'g'), ('c', 't', 'c', 'c'), ('c', 'g', 'a', 'a'), ('c', 'g', 'a', 't'), ('c', 'g', 'a', 'g'), ('c', 'g', 'a', 'c'), ('c', 'g', 't', 'a'), ('c', 'g', 't', 't'), ('c', 'g', 't', 'g'), ('c', 'g', 't', 'c'), ('c', 'g', 'g', 'a'), ('c', 'g', 'g', 't'), ('c', 'g', 'g', 'g'), ('c', 'g', 'g', 'c'), ('c', 'g', 'c', 'a'), ('c', 'g', 'c', 't'), ('c', 'g', 'c', 'g'), ('c', 'g', 'c', 'c'), ('c', 'c', 'a', 'a'), ('c', 'c', 'a', 't'), ('c', 'c', 'a', 'g'), ('c', 'c', 'a', 'c'), ('c', 'c', 't', 'a'), ('c', 'c', 't', 't'), ('c', 'c', 't', 'g'), ('c', 'c', 't', 'c'), ('c', 'c', 'g', 'a'), ('c', 'c', 'g', 't'), ('c', 'c', 'g', 'g'), ('c', 'c', 'g', 'c'), ('c', 'c', 'c', 'a'), ('c', 'c', 'c', 't'), ('c', 'c', 'c', 'g'), ('c', 'c', 'c', 'c')]
Mind that itertools usually work lazily: they do return an iterator. Since O(mn) usually blows up fast, it can therefore be useful to use a generator instead of constructing a list: in that case at least you save on memory. Furthermore if n is large (like 16 or larger for m=4), usually a computer will start having difficulty processing the elements.
Guess I was beaten while trying this out .. will leave it up just for my statistics.
If what you want is actually all possible permutations (i.e aaaa, aaat, aaag, aaac... ), you can use itertools this way:
from itertools import product
print(list(product('atgc', repeat=4)))
There is a python function for generating combinations from a list:
itertools.combinations
A person was trying to list all combinations of a list taken two at a time in this post: Python - list the combination pair for a function value
As a student's exercise, your code is readable and does what you want.
I guess the question is, why do you need all these combinations? Practical bioinformatics is, among other things, a mess of file types and formats, and you'll probably encounter some input data using a different alphabet than the one you're working with.
Regarding modules, there are two general-purpose I'll mention. The rest really depends on what specific task you're trying to accomplish. Biopython is the more mature and widely supported, but the code base is it's showing it's age. scikit-bio is the new kid on the block with beautiful, fully tested code, but with less features and less support for obscure file formats.
list comprehension:
proteins = ['a', 't', 'c', 'g']
all_combos = [x+y for x in proteins for y in proteins]

Python: How to generate list of every possible permutation of three characters given a length? [duplicate]

This question already has answers here:
Generating permutations with repetitions
(6 answers)
Closed 7 years ago.
Say I have a string "ABC"
I'd like to iterate over every possibility of the characters 'A', 'B' and 'C' in a string 5 characters long.
Desired output:
['A', 'A', 'A', 'A', 'A' ]
['A', 'A', 'A', 'A', 'B' ]
...
['C', 'C', 'C', 'C', 'C' ]
I tried using [x for x in itertools.permutations('ABC', r=5)] but it just returns an empty list.
You need itertools.product :
>>> list(product('ABC',repeat=5))
[('A', 'A', 'A', 'A', 'A'), ('A', 'A', 'A', 'A', 'B'), ('A', 'A', 'A', 'A', 'C'), ('A', 'A', 'A', 'B', 'A'), ('A', 'A', 'A', 'B', 'B'), ('A', 'A', 'A', 'B', 'C'), ('A', 'A', 'A', 'C', 'A'), ('A', 'A', 'A', 'C', 'B'), ('A', 'A', 'A', 'C', 'C'), ('A', 'A', 'B', 'A', 'A'), ('A', 'A', 'B', 'A', 'B'), ('A', 'A', 'B', 'A', 'C'), ('A', 'A', 'B', 'B', 'A'), ('A', 'A', 'B', 'B', 'B'), ('A', 'A', 'B', 'B', 'C'), ('A', 'A', 'B', 'C', 'A'), ('A', 'A', 'B', 'C', 'B'), ('A', 'A', 'B', 'C', 'C'), ('A', 'A', 'C', 'A', 'A'), ('A', 'A', 'C', 'A', 'B'), ('A', 'A', 'C', 'A', 'C'), ('A', 'A', 'C', 'B', 'A'), ('A', 'A', 'C', 'B', 'B'), ('A', 'A', 'C', 'B', 'C'), ('A', 'A', 'C', 'C', 'A'), ('A', 'A', 'C', 'C', 'B'), ('A', 'A', 'C', 'C', 'C'), ('A', 'B', 'A', 'A', 'A'), ('A', 'B', 'A', 'A', 'B'), ('A', 'B', 'A', 'A', 'C'), ('A', 'B', 'A', 'B', 'A'), ('A', 'B', 'A', 'B', 'B'), ('A', 'B', 'A', 'B', 'C'), ('A', 'B', 'A', 'C', 'A'), ('A', 'B', 'A', 'C', 'B'), ('A', 'B', 'A', 'C', 'C'), ('A', 'B', 'B', 'A', 'A'), ('A', 'B', 'B', 'A', 'B'), ('A', 'B', 'B', 'A', 'C'), ('A', 'B', 'B', 'B', 'A'), ('A', 'B', 'B', 'B', 'B'), ('A', 'B', 'B', 'B', 'C'), ('A', 'B', 'B', 'C', 'A'), ('A', 'B', 'B', 'C', 'B'), ('A', 'B', 'B', 'C', 'C'), ('A', 'B', 'C', 'A', 'A'), ('A', 'B', 'C', 'A', 'B'), ('A', 'B', 'C', 'A', 'C'), ('A', 'B', 'C', 'B', 'A'), ('A', 'B', 'C', 'B', 'B'), ('A', 'B', 'C', 'B', 'C'), ('A', 'B', 'C', 'C', 'A'), ('A', 'B', 'C', 'C', 'B'), ('A', 'B', 'C', 'C', 'C'), ('A', 'C', 'A', 'A', 'A'), ('A', 'C', 'A', 'A', 'B'), ('A', 'C', 'A', 'A', 'C'), ('A', 'C', 'A', 'B', 'A'), ('A', 'C', 'A', 'B', 'B'), ('A', 'C', 'A', 'B', 'C'), ('A', 'C', 'A', 'C', 'A'), ('A', 'C', 'A', 'C', 'B'), ('A', 'C', 'A', 'C', 'C'), ('A', 'C', 'B', 'A', 'A'), ('A', 'C', 'B', 'A', 'B'), ('A', 'C', 'B', 'A', 'C'), ('A', 'C', 'B', 'B', 'A'), ('A', 'C', 'B', 'B', 'B'), ('A', 'C', 'B', 'B', 'C'), ('A', 'C', 'B', 'C', 'A'), ('A', 'C', 'B', 'C', 'B'), ('A', 'C', 'B', 'C', 'C'), ('A', 'C', 'C', 'A', 'A'), ('A', 'C', 'C', 'A', 'B'), ('A', 'C', 'C', 'A', 'C'), ('A', 'C', 'C', 'B', 'A'), ('A', 'C', 'C', 'B', 'B'), ('A', 'C', 'C', 'B', 'C'), ('A', 'C', 'C', 'C', 'A'), ('A', 'C', 'C', 'C', 'B'), ('A', 'C', 'C', 'C', 'C'), ('B', 'A', 'A', 'A', 'A'), ('B', 'A', 'A', 'A', 'B'), ('B', 'A', 'A', 'A', 'C'), ('B', 'A', 'A', 'B', 'A'), ('B', 'A', 'A', 'B', 'B'), ('B', 'A', 'A', 'B', 'C'), ('B', 'A', 'A', 'C', 'A'), ('B', 'A', 'A', 'C', 'B'), ('B', 'A', 'A', 'C', 'C'), ('B', 'A', 'B', 'A', 'A'), ('B', 'A', 'B', 'A', 'B'), ('B', 'A', 'B', 'A', 'C'), ('B', 'A', 'B', 'B', 'A'), ('B', 'A', 'B', 'B', 'B'), ('B', 'A', 'B', 'B', 'C'), ('B', 'A', 'B', 'C', 'A'), ('B', 'A', 'B', 'C', 'B'), ('B', 'A', 'B', 'C', 'C'), ('B', 'A', 'C', 'A', 'A'), ('B', 'A', 'C', 'A', 'B'), ('B', 'A', 'C', 'A', 'C'), ('B', 'A', 'C', 'B', 'A'), ('B', 'A', 'C', 'B', 'B'), ('B', 'A', 'C', 'B', 'C'), ('B', 'A', 'C', 'C', 'A'), ('B', 'A', 'C', 'C', 'B'), ('B', 'A', 'C', 'C', 'C'), ('B', 'B', 'A', 'A', 'A'), ('B', 'B', 'A', 'A', 'B'), ('B', 'B', 'A', 'A', 'C'), ('B', 'B', 'A', 'B', 'A'), ('B', 'B', 'A', 'B', 'B'), ('B', 'B', 'A', 'B', 'C'), ('B', 'B', 'A', 'C', 'A'), ('B', 'B', 'A', 'C', 'B'), ('B', 'B', 'A', 'C', 'C'), ('B', 'B', 'B', 'A', 'A'), ('B', 'B', 'B', 'A', 'B'), ('B', 'B', 'B', 'A', 'C'), ('B', 'B', 'B', 'B', 'A'), ('B', 'B', 'B', 'B', 'B'), ('B', 'B', 'B', 'B', 'C'), ('B', 'B', 'B', 'C', 'A'), ('B', 'B', 'B', 'C', 'B'), ('B', 'B', 'B', 'C', 'C'), ('B', 'B', 'C', 'A', 'A'), ('B', 'B', 'C', 'A', 'B'), ('B', 'B', 'C', 'A', 'C'), ('B', 'B', 'C', 'B', 'A'), ('B', 'B', 'C', 'B', 'B'), ('B', 'B', 'C', 'B', 'C'), ('B', 'B', 'C', 'C', 'A'), ('B', 'B', 'C', 'C', 'B'), ('B', 'B', 'C', 'C', 'C'), ('B', 'C', 'A', 'A', 'A'), ('B', 'C', 'A', 'A', 'B'), ('B', 'C', 'A', 'A', 'C'), ('B', 'C', 'A', 'B', 'A'), ('B', 'C', 'A', 'B', 'B'), ('B', 'C', 'A', 'B', 'C'), ('B', 'C', 'A', 'C', 'A'), ('B', 'C', 'A', 'C', 'B'), ('B', 'C', 'A', 'C', 'C'), ('B', 'C', 'B', 'A', 'A'), ('B', 'C', 'B', 'A', 'B'), ('B', 'C', 'B', 'A', 'C'), ('B', 'C', 'B', 'B', 'A'), ('B', 'C', 'B', 'B', 'B'), ('B', 'C', 'B', 'B', 'C'), ('B', 'C', 'B', 'C', 'A'), ('B', 'C', 'B', 'C', 'B'), ('B', 'C', 'B', 'C', 'C'), ('B', 'C', 'C', 'A', 'A'), ('B', 'C', 'C', 'A', 'B'), ('B', 'C', 'C', 'A', 'C'), ('B', 'C', 'C', 'B', 'A'), ('B', 'C', 'C', 'B', 'B'), ('B', 'C', 'C', 'B', 'C'), ('B', 'C', 'C', 'C', 'A'), ('B', 'C', 'C', 'C', 'B'), ('B', 'C', 'C', 'C', 'C'), ('C', 'A', 'A', 'A', 'A'), ('C', 'A', 'A', 'A', 'B'), ('C', 'A', 'A', 'A', 'C'), ('C', 'A', 'A', 'B', 'A'), ('C', 'A', 'A', 'B', 'B'), ('C', 'A', 'A', 'B', 'C'), ('C', 'A', 'A', 'C', 'A'), ('C', 'A', 'A', 'C', 'B'), ('C', 'A', 'A', 'C', 'C'), ('C', 'A', 'B', 'A', 'A'), ('C', 'A', 'B', 'A', 'B'), ('C', 'A', 'B', 'A', 'C'), ('C', 'A', 'B', 'B', 'A'), ('C', 'A', 'B', 'B', 'B'), ('C', 'A', 'B', 'B', 'C'), ('C', 'A', 'B', 'C', 'A'), ('C', 'A', 'B', 'C', 'B'), ('C', 'A', 'B', 'C', 'C'), ('C', 'A', 'C', 'A', 'A'), ('C', 'A', 'C', 'A', 'B'), ('C', 'A', 'C', 'A', 'C'), ('C', 'A', 'C', 'B', 'A'), ('C', 'A', 'C', 'B', 'B'), ('C', 'A', 'C', 'B', 'C'), ('C', 'A', 'C', 'C', 'A'), ('C', 'A', 'C', 'C', 'B'), ('C', 'A', 'C', 'C', 'C'), ('C', 'B', 'A', 'A', 'A'), ('C', 'B', 'A', 'A', 'B'), ('C', 'B', 'A', 'A', 'C'), ('C', 'B', 'A', 'B', 'A'), ('C', 'B', 'A', 'B', 'B'), ('C', 'B', 'A', 'B', 'C'), ('C', 'B', 'A', 'C', 'A'), ('C', 'B', 'A', 'C', 'B'), ('C', 'B', 'A', 'C', 'C'), ('C', 'B', 'B', 'A', 'A'), ('C', 'B', 'B', 'A', 'B'), ('C', 'B', 'B', 'A', 'C'), ('C', 'B', 'B', 'B', 'A'), ('C', 'B', 'B', 'B', 'B'), ('C', 'B', 'B', 'B', 'C'), ('C', 'B', 'B', 'C', 'A'), ('C', 'B', 'B', 'C', 'B'), ('C', 'B', 'B', 'C', 'C'), ('C', 'B', 'C', 'A', 'A'), ('C', 'B', 'C', 'A', 'B'), ('C', 'B', 'C', 'A', 'C'), ('C', 'B', 'C', 'B', 'A'), ('C', 'B', 'C', 'B', 'B'), ('C', 'B', 'C', 'B', 'C'), ('C', 'B', 'C', 'C', 'A'), ('C', 'B', 'C', 'C', 'B'), ('C', 'B', 'C', 'C', 'C'), ('C', 'C', 'A', 'A', 'A'), ('C', 'C', 'A', 'A', 'B'), ('C', 'C', 'A', 'A', 'C'), ('C', 'C', 'A', 'B', 'A'), ('C', 'C', 'A', 'B', 'B'), ('C', 'C', 'A', 'B', 'C'), ('C', 'C', 'A', 'C', 'A'), ('C', 'C', 'A', 'C', 'B'), ('C', 'C', 'A', 'C', 'C'), ('C', 'C', 'B', 'A', 'A'), ('C', 'C', 'B', 'A', 'B'), ('C', 'C', 'B', 'A', 'C'), ('C', 'C', 'B', 'B', 'A'), ('C', 'C', 'B', 'B', 'B'), ('C', 'C', 'B', 'B', 'C'), ('C', 'C', 'B', 'C', 'A'), ('C', 'C', 'B', 'C', 'B'), ('C', 'C', 'B', 'C', 'C'), ('C', 'C', 'C', 'A', 'A'), ('C', 'C', 'C', 'A', 'B'), ('C', 'C', 'C', 'A', 'C'), ('C', 'C', 'C', 'B', 'A'), ('C', 'C', 'C', 'B', 'B'), ('C', 'C', 'C', 'B', 'C'), ('C', 'C', 'C', 'C', 'A'), ('C', 'C', 'C', 'C', 'B'), ('C', 'C', 'C', 'C', 'C')]

Categories