Getting all possible combinations of list items

Martin von Loewis loewis at informatik.hu-berlin.de
Mon Sep 17 05:11:50 EDT 2001


"Greg Krohn" <volucris at hotmail.com> 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
        result.append(number)
    return result

Regards,
Martin



More information about the Python-list mailing list