Question: I want to create all possible permutations of tuples in a list using python. For example, take the list my_list = [(None, 1), (None,1), (None,1)]  I want the generate a list of (2!)^n lists where 2! is the number of combinations of the tuple arrangements and n represents the number of tuples in the list. So the above list should produce 8 distinct lists, for example. I can achieve this if I use the itertools python module in the following way: from itertools import permutations my_list = [(None, 1), (None, 1), (None, 1)] all_permutations = [] for k in permutations(my_list[0]): for j in permutations(my_list[1]): for i in permutations(my_list[2]): all_permutations.append([k,j,i]) print(all_permutations)  and I get the expected output: [[(None, 1), (None, 1), (None, 1)], [(None, 1), (None, 1), (1, None)], [(None, 1), (1, None), (None, 1)], [(None, 1), (1, None), (1, None)], [(1, None), (None, 1), (None, 1)], [(1, None), (None, 1), (1, None)], [(1, None), (1, None), (None, 1)], [(1, None), (1, None), (1, None)]]  How do I generalize this result to a list of n tuples of size 2 without having to hard code the for-loop? The closest thing I've seen was this question, but it doesn't quite do what I need. Any suggestions would be greatly appreciated!

Answer: itertools.product is useful here (if I understand your question correctly). itertools.product is the equivalent to your nested loops and takes as argument multiple iterables. So [permutations(x) for x in my_list] (or map(permutations, my_list)) generically creates a list of iterables from my_list and the asterisk * turns that into separate arguments for product. >>> from itertools import permutations, product >>> my_list = [(None, 1), (None, 1), (None, 1)] >>> list(product(*[permutations(x) for x in my_list])) [((None, 1), (None, 1), (None, 1)), ((None, 1), (None, 1), (1, None)), ((None, 1), (1, None), (None, 1)), ((None, 1), (1, None), (1, None)), ((1, None), (None, 1), (None, 1)), ((1, None), (None, 1), (1, None)), ((1, None), (1, None), (None, 1)), ((1, None), (1, None), (1, None))] >>> my_list = [(None, 1), ("a", "b"), (1,2,3), (None, 4)] >>> list(product(*[permutations(x) for x in my_list])) [((None, 1), ('a', 'b'), (1, 2, 3), (None, 4)), ((None, 1), ('a', 'b'), (1, 2, 3), (4, None)), ((None, 1), ('a', 'b'), (1, 3, 2), (None, 4)), ((None, 1), ('a', 'b'), (1, 3, 2), (4, None)), ((None, 1), ('a', 'b'), (2, 1, 3), (None, 4)), ((None, 1), ('a', 'b'), (2, 1, 3), (4, None)), ((None, 1), ('a', 'b'), (2, 3, 1), (None, 4)), ((None, 1), ('a', 'b'), (2, 3, 1), (4, None)), ((None, 1), ('a', 'b'), (3, 1, 2), (None, 4)), ((None, 1), ('a', 'b'), (3, 1, 2), (4, None)), ((None, 1), ('a', 'b'), (3, 2, 1), (None, 4)), ((None, 1), ('a', 'b'), (3, 2, 1), (4, None)), ((None, 1), ('b', 'a'), (1, 2, 3), (None, 4)), ((None, 1), ('b', 'a'), (1, 2, 3), (4, None)), ((None, 1), ('b', 'a'), (1, 3, 2), (None, 4)), ((None, 1), ('b', 'a'), (1, 3, 2), (4, None)), ((None, 1), ('b', 'a'), (2, 1, 3), (None, 4)), ((None, 1), ('b', 'a'), (2, 1, 3), (4, None)), ((None, 1), ('b', 'a'), (2, 3, 1), (None, 4)), ((None, 1), ('b', 'a'), (2, 3, 1), (4, None)), ((None, 1), ('b', 'a'), (3, 1, 2), (None, 4)), ((None, 1), ('b', 'a'), (3, 1, 2), (4, None)), ((None, 1), ('b', 'a'), (3, 2, 1), (None, 4)), ((None, 1), ('b', 'a'), (3, 2, 1), (4, None)), ((1, None), ('a', 'b'), (1, 2, 3), (None, 4)), ((1, None), ('a', 'b'), (1, 2, 3), (4, None)), ((1, None), ('a', 'b'), (1, 3, 2), (None, 4)), ((1, None), ('a', 'b'), (1, 3, 2), (4, None)), ((1, None), ('a', 'b'), (2, 1, 3), (None, 4)), ((1, None), ('a', 'b'), (2, 1, 3), (4, None)), ((1, None), ('a', 'b'), (2, 3, 1), (None, 4)), ((1, None), ('a', 'b'), (2, 3, 1), (4, None)), ((1, None), ('a', 'b'), (3, 1, 2), (None, 4)), ((1, None), ('a', 'b'), (3, 1, 2), (4, None)), ((1, None), ('a', 'b'), (3, 2, 1), (None, 4)), ((1, None), ('a', 'b'), (3, 2, 1), (4, None)), ((1, None), ('b', 'a'), (1, 2, 3), (None, 4)), ((1, None), ('b', 'a'), (1, 2, 3), (4, None)), ((1, None), ('b', 'a'), (1, 3, 2), (None, 4)), ((1, None), ('b', 'a'), (1, 3, 2), (4, None)), ((1, None), ('b', 'a'), (2, 1, 3), (None, 4)), ((1, None), ('b', 'a'), (2, 1, 3), (4, None)), ((1, None), ('b', 'a'), (2, 3, 1), (None, 4)), ((1, None), ('b', 'a'), (2, 3, 1), (4, None)), ((1, None), ('b', 'a'), (3, 1, 2), (None, 4)), ((1, None), ('b', 'a'), (3, 1, 2), (4, None)), ((1, None), ('b', 'a'), (3, 2, 1), (None, 4)), ((1, None), ('b', 'a'), (3, 2, 1), (4, None))] >>> list(product(*map(permutations, my_list)) [((None, 1), ('a', 'b'), (1, 2, 3), (None, 4)), ((None, 1), ('a', 'b'), (1, 2, 3), (4, None)), ((None, 1), ('a', 'b'), (1, 3, 2), (None, 4)), [same output...]  Unlike your example, the above returns a list of tuples (and not a list of lists). Imho, tuples are more appropriate, but if you insist that you need lists, you can of course also convert them, using: [list(tup) for tup in product(*[permutations(x) for x in my_list])]  or list(map(list, product(*map(permutations, my_list))))