Is there any smart way to write a list comprehension over more than one list?
I know I could use a separate range list as index but this way I have to know the length (or get it separately with a len()
function call).
>>> a = range(10)
>>> b = range(10, 0, -1)
>>> [(a[x],b[x]) for x in range(10)]
[(0, 10), (1, 9), (2, 8), (3, 7), (4, 6), (5, 5), (6, 4), (7, 3), (8, 2), (9, 1)]
I'd love to have something like this:
>>> [(a,b) for a in range(10) and b in range(10, 0, -1)]
[(0, 10), (1, 9), (2, 8), (3, 7), (4, 6), (5, 5), (6, 4), (7, 3), (8, 2), (9, 1)]
How would you write the list comprehension? Is there a way to do this with itertools?
The range list just stand for any list and I do not necessarily want to get a tuple. there could also be a function which takes a and b as parameters. So zip is not what I want.
UPDATE: With "So zip is not what I want." I meant that I don't want zip(range(10), range(10, 0, -1))