Python - how to generate wordlist from given characters of specific length

Aamu picture Aamu · Feb 4, 2014 · Viewed 21.2k times · Source

I want to perform a dictionary attack and for that I need word lists. How to generate word list from given characters of specific length ( or word length from min length to max length )? I have tried itertools.combinations_with_replacements and itertools.permutations, but it does not help. They does not have all the word lists that it should return. Any help will be greatly appreciated. Thank you.

Answer

falsetru picture falsetru · Feb 4, 2014

Use itertools.product:

>>> import itertools
>>>
>>> chrs = 'abc'
>>> n = 2
>>>
>>> for xs in itertools.product(chrs, repeat=n):
...     print ''.join(xs)
...
aa
ab
ac
ba
bb
bc
ca
cb
cc

To get word from min length to max length:

chrs = 'abc'
min_length, max_length = 2, 5    
for n in range(min_length, max_length+1):
    for xs in itertools.product(chrs, repeat=n):
        print ''.join(xs)