Enumerating all n-tuples over a finite domain
Use recursion.  Something like this:

def f(l, k):
    if l==0:
        yield []
    else:
        for i in range(k):
            for L in f(l-1, k):
                yield [i]+L


Here f(l, k) produces all lists of length l all of whose entries are in the range 0..k-1.  The idea is that if you have all lists of length l-1 (the output of f(l-1, k)) you can use that to generate all lists of length l, by using a for-loop.

If you're not familiar with that Python syntax, try this variant:

def f(l, k):
    if l==0:
        return [[]]
    t = [] 
    for i in range(k):
        for L in f(l-1, k):
            t.append([i]+L)
    return t


Note that [i] is a list containing a single element, i, and + is the list concatenation operator, so [i] + L is a list obtained by prepending the element i to the front of list L.