3 ms·
It's useful for working with permutations. E.g, [f(a, b) for a in some_list for b in some_list] or even [f(a, b) for a in some_list for b in some_lis
by jetrink 8y ago
It's useful for working with permutations. E.g,
[f(a, b) for a in some_list for b in some_list]
or even
[f(a, b) for a in some_list for b in some_list if a is not b]
- kbp 8y agoitertools has functions that make that sort of nesting unnecessary (and some would say make the code clearer), eg: import itertools as it [f(a, b) for a, b in it.product(some_list, some_list) if a is not b]
- deleted 8y ago[deleted]
- kbp 8y agoOh, or more directly: [f(a, b) for a, b in it.permutations(some_list, r=2)] And then you could also use itertools.starmap instead of using a comprehension at all, if you wanted.