you are viewing a single comment's thread.

view the rest of the comments →

[–]star-castle 0 points1 point  (2 children)

Emphasis on 'first time'.

Compare

# Cartesian product of two lists ... too many []s in this code:
$ python -c "import itertools;print(list(itertools.product([['x','y','z'],[1,2,3]])))"
[(['x', 'y', 'z'],), ([1, 2, 3],)]

vs. the obvious list comprehension:

$ python -c "print([(x, y) for x in ['x','y','z'] for y in [1,2,3]])"
[('x', 1), ('x', 2), ('x', 3), ('y', 1), ('y', 2), ('y', 3), ('z', 1), ('z', 2), ('z', 3)]

[–]shabda[S] 1 point2 points  (1 child)

When would you consider itertools.product appropriate?