All possible lists from ups and downs between elements in python list I have a function that compares sequential elements from a python list and returns 1 and -1:

&gt;&gt;&gt; up_down([0, 2, 1, 3])
[1, -1, 1]


I need a function to return all possible lists from a '-1, 1' list using the function up_down.

&gt;&gt;&gt; possible_lists([1, -1])
[[0, 2, 1], [1, 0, 2]]


I'd like to know if I can write these functions in a better way. My code is below:

import itertools

def comparison(a, b):
    if b &gt; a:
        return 1
    if b &lt; a:
        return -1
    else:
        return 0

def up_down(data, n=1):
    return [comparison(data[pos], data[pos + n]) for pos, value in enumerate(data[:-n])]

def possible_lists(data, n=1):
    size = len(data) + n
    all_lists = itertools.permutations(range(size), size)

    return [list(el) for el in all_lists if up_down(el, n) == data]
Your comparison function is the same as the builtin cmp function.

Your filtering technique to find the matching lists may be not the most efficient. Permutations will generate a lot of lists which fail the test. On the other hand, itertools is written in C, which means it'll probably be faster for small data sets.