how to create a list sequence in python containing all possible pairs an equal number of times?
Question: Given the letters `x,y` is there a way to write a python function that returns
a list sequence containing all possible pairs (`[x,x], [x,y], [y,x], [y,y]`)
an equal number of times?

for example can I write a function that takes the input (eg `[x,y]`) and the
number of times each possible pair should appear (eg `2`), and then returns,
for example, the list sequence: `[x x x y y x y y x]` ?

Preferably I would like the function to generate a "random" sequence so it
could also return:

    
    
    [y x y y x x x y y]
    

In both cases `[x,x] [x,y] [y,x]` and `[y,y]` appear exactly twice.

Ideally, I would like to find a solution that also worked with for example 4
letters `[x,y,z,w]`.


Answer: Based on your edits, the following should conduct the tour to which you were
referring.

    
    
    import random
    import itertools
    
    def generate_sequence(alphabet, num_repeats):
    
        # generate the permutations of the list of size 2
        # repeated `num_repeats` times
        perms = [(letter1, letter2) for letter1, letter2 in itertools.product(alphabet, alphabet) for i in xrange(num_repeats)]
    
        # save the original length of `perm` for later
        perm_len = len(perms)
    
        # randomly choose a starting point and add it to our result
        curr_s, curr_e = random.choice(perms)
        result = [curr_s, curr_e]
    
        # remove the starting point from the list... on to the next one
        perms.remove((curr_s, curr_e))
    
        # while we still have pairs in `perms`...
        while len(perms):
    
            # get all possible next pairs if the end of the current pair
            # equals the beginning of the next pair
            next_vals = [(s,e) for s,e in perms if s == curr_e]
    
            # if there aren't anymore, we may have exhausted the pairs that
            # start with `curr_e`, in which case choose a new random pair
            if len(next_vals) != 0:
                next_s, next_e = random.choice(next_vals)
            else:
                next_s, next_e = random.choice(perms)
    
            # remove the next pair from `perm` and append it to the `result`
            perms.remove((next_s, next_e))
            result.append(next_e)
    
            # set the current pair to the next pair and continue iterating...
            curr_s, curr_e = next_s, next_e
    
        return result
    
    alphabet = ('x', 'y', 'z')
    num_repeats = 2
    print generate_sequence(alphabet, num_repeats)
    

This outputs

    
    
    ['z', 'z', 'z', 'x', 'x', 'y', 'z', 'y', 'y', 'z', 'y', 'x', 'y', 'x', 'z', 'x', 
     'z', 'y', 'x']
    

