Getting all possible combinations of list items

Martin von Loewis loewis at
Mon Sep 17 11:11:50 CEST 2001

"Greg Krohn" <volucris at> writes:

> scratching my head over 'magic' code,

I prefer explicit loops over map and lambdas:

def magic_algorithm(another_list, count):
    length = len(another_list)
    result = []
    for i in xrange(length**count):
        number = ''
        for pos in range(count):
            # prepend the next digit
            number = another_list[i % length] + number
            i = i / length
    return result


More information about the Python-list mailing list