Unique combinations of a list of tuplesPicking unordered combinations from pools with overlapHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?What's the difference between lists and tuples?How to make a flat list out of list of listsHow do I concatenate two lists in Python?How to clone or copy a list?What are “named tuples” in Python?How to sort (list/tuple) of lists/tuples by the element at a given index?How do I list all files of a directory?

Are any jet engines used in combat aircraft water cooled?

What word can be used to describe a bug in a movie?

How do I calculate the difference in lens reach between a superzoom compact and a DSLR zoom lens?

show stdout containing n with line breaks

First amendment and employment: Can an employer terminate you for speech?

Look mom! I made my own (Base 10) numeral system!

Ordering a word list

As a 16 year old, how can I keep my money safe from my mother?

How do I explain to a team that the project they will work on for six months will certainly be cancelled?

Drawing complex inscribed and circumscribed polygons in TikZ

Write an interpreter for *

How can I iterate this process?

Is TA-ing worth the opportunity cost?

How can I tell if a flight itinerary is fake?

Why are Gatwick's runways too close together?

Who are these characters/superheroes in the posters from Chris's room in Family Guy?

Looking for a new job because of relocation - is it okay to tell the real reason?

Why doesn't the "ch" pronunciation rule occur for words such as "durch" and "manchmal"?

Blocking people from taking pictures of me with smartphone

'sudo apt-get update' get a warning

Are there any differences in causality between linear and logistic regression?

Ex-contractor published company source code and secrets online

Best gun to modify into a monsterhunter weapon?

Author changing name



Unique combinations of a list of tuples


Picking unordered combinations from pools with overlapHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?What's the difference between lists and tuples?How to make a flat list out of list of listsHow do I concatenate two lists in Python?How to clone or copy a list?What are “named tuples” in Python?How to sort (list/tuple) of lists/tuples by the element at a given index?How do I list all files of a directory?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








7















Given a list of 3-tuples, for example:[(1,2,3), (4,5,6), (7,8,9)] how would you compute all possible combinations and combinations of subsets?



In this case the result should look like this:



[
(1), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9), (1,4,7), (1,4,8), (1,4,9), (1,5,7), (1,5,8), (1,5,9), (1,6,7), (1,6,8), (1,6,9),
(2), ...,
(3), ...,
(4), (4,7), (4,8), (4,9),
(5), (5,7), (5,8), (5,9),
(6), (6,7), (6,8), (6,9),
(7), (8), (9)
]



  • all tuples with identical elements are regarded the same

  • combinations which derive from the same tuples are not allowed (e.g. these shouldn't be in the solution: (1,2), (4,6) or (7,8,9))









share|improve this question
























  • But wait, why (1) to (9) are part of the soultion if (1,2) is not allowed given the second rule ?

    – Drey
    7 hours ago











  • It looks like there are three sets of tuples: 1) [(x,) for x in the_list[0]], 2) [(x,y) for x in the_list[0] for y in the_list[1]], and 3) [(x,y,z) for x in the_list[0] for y in the_list[1] for z in the_list[2]].

    – chepner
    7 hours ago











  • Possible duplicate of Picking unordered combinations from pools with overlap

    – Joseph Wood
    6 hours ago

















7















Given a list of 3-tuples, for example:[(1,2,3), (4,5,6), (7,8,9)] how would you compute all possible combinations and combinations of subsets?



In this case the result should look like this:



[
(1), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9), (1,4,7), (1,4,8), (1,4,9), (1,5,7), (1,5,8), (1,5,9), (1,6,7), (1,6,8), (1,6,9),
(2), ...,
(3), ...,
(4), (4,7), (4,8), (4,9),
(5), (5,7), (5,8), (5,9),
(6), (6,7), (6,8), (6,9),
(7), (8), (9)
]



  • all tuples with identical elements are regarded the same

  • combinations which derive from the same tuples are not allowed (e.g. these shouldn't be in the solution: (1,2), (4,6) or (7,8,9))









share|improve this question
























  • But wait, why (1) to (9) are part of the soultion if (1,2) is not allowed given the second rule ?

    – Drey
    7 hours ago











  • It looks like there are three sets of tuples: 1) [(x,) for x in the_list[0]], 2) [(x,y) for x in the_list[0] for y in the_list[1]], and 3) [(x,y,z) for x in the_list[0] for y in the_list[1] for z in the_list[2]].

    – chepner
    7 hours ago











  • Possible duplicate of Picking unordered combinations from pools with overlap

    – Joseph Wood
    6 hours ago













7












7








7








Given a list of 3-tuples, for example:[(1,2,3), (4,5,6), (7,8,9)] how would you compute all possible combinations and combinations of subsets?



In this case the result should look like this:



[
(1), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9), (1,4,7), (1,4,8), (1,4,9), (1,5,7), (1,5,8), (1,5,9), (1,6,7), (1,6,8), (1,6,9),
(2), ...,
(3), ...,
(4), (4,7), (4,8), (4,9),
(5), (5,7), (5,8), (5,9),
(6), (6,7), (6,8), (6,9),
(7), (8), (9)
]



  • all tuples with identical elements are regarded the same

  • combinations which derive from the same tuples are not allowed (e.g. these shouldn't be in the solution: (1,2), (4,6) or (7,8,9))









share|improve this question














Given a list of 3-tuples, for example:[(1,2,3), (4,5,6), (7,8,9)] how would you compute all possible combinations and combinations of subsets?



In this case the result should look like this:



[
(1), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9), (1,4,7), (1,4,8), (1,4,9), (1,5,7), (1,5,8), (1,5,9), (1,6,7), (1,6,8), (1,6,9),
(2), ...,
(3), ...,
(4), (4,7), (4,8), (4,9),
(5), (5,7), (5,8), (5,9),
(6), (6,7), (6,8), (6,9),
(7), (8), (9)
]



  • all tuples with identical elements are regarded the same

  • combinations which derive from the same tuples are not allowed (e.g. these shouldn't be in the solution: (1,2), (4,6) or (7,8,9))






python tuples combinations permutation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 8 hours ago









p4rchp4rch

626 bronze badges




626 bronze badges















  • But wait, why (1) to (9) are part of the soultion if (1,2) is not allowed given the second rule ?

    – Drey
    7 hours ago











  • It looks like there are three sets of tuples: 1) [(x,) for x in the_list[0]], 2) [(x,y) for x in the_list[0] for y in the_list[1]], and 3) [(x,y,z) for x in the_list[0] for y in the_list[1] for z in the_list[2]].

    – chepner
    7 hours ago











  • Possible duplicate of Picking unordered combinations from pools with overlap

    – Joseph Wood
    6 hours ago

















  • But wait, why (1) to (9) are part of the soultion if (1,2) is not allowed given the second rule ?

    – Drey
    7 hours ago











  • It looks like there are three sets of tuples: 1) [(x,) for x in the_list[0]], 2) [(x,y) for x in the_list[0] for y in the_list[1]], and 3) [(x,y,z) for x in the_list[0] for y in the_list[1] for z in the_list[2]].

    – chepner
    7 hours ago











  • Possible duplicate of Picking unordered combinations from pools with overlap

    – Joseph Wood
    6 hours ago
















But wait, why (1) to (9) are part of the soultion if (1,2) is not allowed given the second rule ?

– Drey
7 hours ago





But wait, why (1) to (9) are part of the soultion if (1,2) is not allowed given the second rule ?

– Drey
7 hours ago













It looks like there are three sets of tuples: 1) [(x,) for x in the_list[0]], 2) [(x,y) for x in the_list[0] for y in the_list[1]], and 3) [(x,y,z) for x in the_list[0] for y in the_list[1] for z in the_list[2]].

– chepner
7 hours ago





It looks like there are three sets of tuples: 1) [(x,) for x in the_list[0]], 2) [(x,y) for x in the_list[0] for y in the_list[1]], and 3) [(x,y,z) for x in the_list[0] for y in the_list[1] for z in the_list[2]].

– chepner
7 hours ago













Possible duplicate of Picking unordered combinations from pools with overlap

– Joseph Wood
6 hours ago





Possible duplicate of Picking unordered combinations from pools with overlap

– Joseph Wood
6 hours ago












5 Answers
5






active

oldest

votes


















3














You can use recursion with a generator:



data = [(1,2,3), (4,5,6), (7,8,9)]
def combos(d, c = []):
if len(c) == len(d):
yield c
else:
for i in d:
if i not in c:
yield from combos(d, c+[i])

def product(d, c = []):
if c:
yield tuple(c)
if d:
for i in d[0]:
yield from product(d[1:], c+[i])

result = sorted(i for b in combos(data) for i in product(b))
final_result = [a for i, a in enumerate(result) if all(len(c) != len(a) or len(set(c)&set(a)) != len(a) for c in result[:i])]


Output:



[(1,), (1, 4), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6), (1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 7), (1, 8), (1, 9), (2,), (2, 4), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 7), (2, 8), (2, 9), (3,), (3, 4), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 7), (3, 8), (3, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]





share|improve this answer


































    3














    Using itertools:



    import itertools as it

    def all_combinations(groups):
    result = set()
    for prod in it.product(*groups):
    for length in range(1, len(groups) + 1):
    result.update(it.combinations(prod, length))
    return result

    all_combinations([(1,2,3), (4,5,6), (7,8,9)])





    share|improve this answer


































      2














      Here is a non-recursive solution with a simple for loop. Uniqueness if enforced by applying set to the list of output tuples.



      lsts = [(1,2,3), (4,5,6), (7,8,9)]

      res = [[]]
      for lst in lsts:
      res += [(*r, x) for r in res for x in lst]

      # print(tuple(lst) for lst in res[1:])
      # (5, 9), (4, 7), (6, 9), (1, 4, 7), (2, 6, 9), (4, 8), (3, 4, 7), (2,
      # 8), (2, 6, 8), (9,), (2, 5, 8), (1, 6), (3, 6, 8), (2, 5, 9), (3, 5,
      # 9), (3, 7), (2, 5), (3, 6, 9), (5, 8), (1, 6, 8), (3, 5, 8), (2, 6,
      # 7), (4, 9), (6, 7), (1,), (2, 9), (1, 6, 9), (3,), (1, 5), (5,), (3,
      # 6), (7,), (3, 6, 7), (1, 5, 9), (2, 6), (2, 4, 7), (1, 5, 8), (3, 4,
      # 8), (8,), (3, 4, 9), (1, 4), (1, 6, 7), (3, 9), (1, 9), (2, 5, 7), (3,
      # 5), (2, 7), (2, 4, 9), (6, 8), (1, 5, 7), (2,), (2, 4, 8), (5, 7), (1,
      # 4, 8), (3, 5, 7), (4,), (3, 8), (1, 8), (1, 4, 9), (6,), (1, 7), (3,
      # 4), (2, 4)





      share|improve this answer

























      • Nice! But don't forget to add singleton solutions (as per strange exception from rule 2)

        – Drey
        5 hours ago











      • Those are in the set, just hard to see :)

        – hilberts_drinking_problem
        5 hours ago











      • Ups, yes, my bad, now I see them :D

        – Drey
        5 hours ago


















      1














      Another version:



      from itertools import product

      lst = [(1,2,3), (4,5,6), (7,8,9)]

      def generate(lst):
      for idx in range(len(lst)):
      for val in lst[idx]:
      yield (val,)
      for i in range(len(lst), idx+1, -1):
      l = tuple((val,) + i for i in product(*lst[idx+1:i]))
      if len(l) > 1:
      yield from l

      l = [*generate(lst)]
      print(l)


      Prints:



      [(1,), (1, 4), (1, 5), (1, 6), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8), (1, 6, 9), (2,), (2, 4), (2, 5), (2, 6), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7), (2, 6, 8), (2, 6, 9), (3,), (3, 4), (3, 5), (3, 6), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 7), (3, 6, 8), (3, 6, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]





      share|improve this answer


































        0














        Oookay, although I don't fully understand the second rule, here is some short solution to the problem



        from itertools import chain, combinations
        blubb = [(1,2,3), (4,5,6), (7,8,9)]
        blubb_as_set = list(map(set, blubb))

        all_blubbs = list(chain.from_iterable(blubb))
        all_blubb_combos = (combinations(all_blubbs, i) for i in range(1, 4))
        as_a_list = list(chain.from_iterable(all_blubb_combos))

        test_subset = lambda x: not any(set(x).issubset(blubb_set) for blubb_set in blubb_as_set)
        list(filter(test_subset, as_a_list))
        # alternative that includes single elements
        list(filter(test_subset, as_a_list)) + list(map(lambda x: (x, ), chain.from_iterable(blubb)))



        For the most parts you can leave out list calls.
        Your can also create different not_allowed cases based on r if you need to deal with tuple's length more than 3.




        Edit: explicit test for subset.






        share|improve this answer





























          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57444657%2funique-combinations-of-a-list-of-tuples%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          You can use recursion with a generator:



          data = [(1,2,3), (4,5,6), (7,8,9)]
          def combos(d, c = []):
          if len(c) == len(d):
          yield c
          else:
          for i in d:
          if i not in c:
          yield from combos(d, c+[i])

          def product(d, c = []):
          if c:
          yield tuple(c)
          if d:
          for i in d[0]:
          yield from product(d[1:], c+[i])

          result = sorted(i for b in combos(data) for i in product(b))
          final_result = [a for i, a in enumerate(result) if all(len(c) != len(a) or len(set(c)&set(a)) != len(a) for c in result[:i])]


          Output:



          [(1,), (1, 4), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6), (1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 7), (1, 8), (1, 9), (2,), (2, 4), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 7), (2, 8), (2, 9), (3,), (3, 4), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 7), (3, 8), (3, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]





          share|improve this answer































            3














            You can use recursion with a generator:



            data = [(1,2,3), (4,5,6), (7,8,9)]
            def combos(d, c = []):
            if len(c) == len(d):
            yield c
            else:
            for i in d:
            if i not in c:
            yield from combos(d, c+[i])

            def product(d, c = []):
            if c:
            yield tuple(c)
            if d:
            for i in d[0]:
            yield from product(d[1:], c+[i])

            result = sorted(i for b in combos(data) for i in product(b))
            final_result = [a for i, a in enumerate(result) if all(len(c) != len(a) or len(set(c)&set(a)) != len(a) for c in result[:i])]


            Output:



            [(1,), (1, 4), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6), (1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 7), (1, 8), (1, 9), (2,), (2, 4), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 7), (2, 8), (2, 9), (3,), (3, 4), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 7), (3, 8), (3, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]





            share|improve this answer





























              3












              3








              3







              You can use recursion with a generator:



              data = [(1,2,3), (4,5,6), (7,8,9)]
              def combos(d, c = []):
              if len(c) == len(d):
              yield c
              else:
              for i in d:
              if i not in c:
              yield from combos(d, c+[i])

              def product(d, c = []):
              if c:
              yield tuple(c)
              if d:
              for i in d[0]:
              yield from product(d[1:], c+[i])

              result = sorted(i for b in combos(data) for i in product(b))
              final_result = [a for i, a in enumerate(result) if all(len(c) != len(a) or len(set(c)&set(a)) != len(a) for c in result[:i])]


              Output:



              [(1,), (1, 4), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6), (1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 7), (1, 8), (1, 9), (2,), (2, 4), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 7), (2, 8), (2, 9), (3,), (3, 4), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 7), (3, 8), (3, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]





              share|improve this answer















              You can use recursion with a generator:



              data = [(1,2,3), (4,5,6), (7,8,9)]
              def combos(d, c = []):
              if len(c) == len(d):
              yield c
              else:
              for i in d:
              if i not in c:
              yield from combos(d, c+[i])

              def product(d, c = []):
              if c:
              yield tuple(c)
              if d:
              for i in d[0]:
              yield from product(d[1:], c+[i])

              result = sorted(i for b in combos(data) for i in product(b))
              final_result = [a for i, a in enumerate(result) if all(len(c) != len(a) or len(set(c)&set(a)) != len(a) for c in result[:i])]


              Output:



              [(1,), (1, 4), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6), (1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 7), (1, 8), (1, 9), (2,), (2, 4), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 7), (2, 8), (2, 9), (3,), (3, 4), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 7), (3, 8), (3, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 7 hours ago

























              answered 8 hours ago









              Ajax1234Ajax1234

              46.6k4 gold badges31 silver badges60 bronze badges




              46.6k4 gold badges31 silver badges60 bronze badges


























                  3














                  Using itertools:



                  import itertools as it

                  def all_combinations(groups):
                  result = set()
                  for prod in it.product(*groups):
                  for length in range(1, len(groups) + 1):
                  result.update(it.combinations(prod, length))
                  return result

                  all_combinations([(1,2,3), (4,5,6), (7,8,9)])





                  share|improve this answer































                    3














                    Using itertools:



                    import itertools as it

                    def all_combinations(groups):
                    result = set()
                    for prod in it.product(*groups):
                    for length in range(1, len(groups) + 1):
                    result.update(it.combinations(prod, length))
                    return result

                    all_combinations([(1,2,3), (4,5,6), (7,8,9)])





                    share|improve this answer





























                      3












                      3








                      3







                      Using itertools:



                      import itertools as it

                      def all_combinations(groups):
                      result = set()
                      for prod in it.product(*groups):
                      for length in range(1, len(groups) + 1):
                      result.update(it.combinations(prod, length))
                      return result

                      all_combinations([(1,2,3), (4,5,6), (7,8,9)])





                      share|improve this answer















                      Using itertools:



                      import itertools as it

                      def all_combinations(groups):
                      result = set()
                      for prod in it.product(*groups):
                      for length in range(1, len(groups) + 1):
                      result.update(it.combinations(prod, length))
                      return result

                      all_combinations([(1,2,3), (4,5,6), (7,8,9)])






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 8 hours ago

























                      answered 8 hours ago









                      eumiroeumiro

                      139k22 gold badges245 silver badges237 bronze badges




                      139k22 gold badges245 silver badges237 bronze badges
























                          2














                          Here is a non-recursive solution with a simple for loop. Uniqueness if enforced by applying set to the list of output tuples.



                          lsts = [(1,2,3), (4,5,6), (7,8,9)]

                          res = [[]]
                          for lst in lsts:
                          res += [(*r, x) for r in res for x in lst]

                          # print(tuple(lst) for lst in res[1:])
                          # (5, 9), (4, 7), (6, 9), (1, 4, 7), (2, 6, 9), (4, 8), (3, 4, 7), (2,
                          # 8), (2, 6, 8), (9,), (2, 5, 8), (1, 6), (3, 6, 8), (2, 5, 9), (3, 5,
                          # 9), (3, 7), (2, 5), (3, 6, 9), (5, 8), (1, 6, 8), (3, 5, 8), (2, 6,
                          # 7), (4, 9), (6, 7), (1,), (2, 9), (1, 6, 9), (3,), (1, 5), (5,), (3,
                          # 6), (7,), (3, 6, 7), (1, 5, 9), (2, 6), (2, 4, 7), (1, 5, 8), (3, 4,
                          # 8), (8,), (3, 4, 9), (1, 4), (1, 6, 7), (3, 9), (1, 9), (2, 5, 7), (3,
                          # 5), (2, 7), (2, 4, 9), (6, 8), (1, 5, 7), (2,), (2, 4, 8), (5, 7), (1,
                          # 4, 8), (3, 5, 7), (4,), (3, 8), (1, 8), (1, 4, 9), (6,), (1, 7), (3,
                          # 4), (2, 4)





                          share|improve this answer

























                          • Nice! But don't forget to add singleton solutions (as per strange exception from rule 2)

                            – Drey
                            5 hours ago











                          • Those are in the set, just hard to see :)

                            – hilberts_drinking_problem
                            5 hours ago











                          • Ups, yes, my bad, now I see them :D

                            – Drey
                            5 hours ago















                          2














                          Here is a non-recursive solution with a simple for loop. Uniqueness if enforced by applying set to the list of output tuples.



                          lsts = [(1,2,3), (4,5,6), (7,8,9)]

                          res = [[]]
                          for lst in lsts:
                          res += [(*r, x) for r in res for x in lst]

                          # print(tuple(lst) for lst in res[1:])
                          # (5, 9), (4, 7), (6, 9), (1, 4, 7), (2, 6, 9), (4, 8), (3, 4, 7), (2,
                          # 8), (2, 6, 8), (9,), (2, 5, 8), (1, 6), (3, 6, 8), (2, 5, 9), (3, 5,
                          # 9), (3, 7), (2, 5), (3, 6, 9), (5, 8), (1, 6, 8), (3, 5, 8), (2, 6,
                          # 7), (4, 9), (6, 7), (1,), (2, 9), (1, 6, 9), (3,), (1, 5), (5,), (3,
                          # 6), (7,), (3, 6, 7), (1, 5, 9), (2, 6), (2, 4, 7), (1, 5, 8), (3, 4,
                          # 8), (8,), (3, 4, 9), (1, 4), (1, 6, 7), (3, 9), (1, 9), (2, 5, 7), (3,
                          # 5), (2, 7), (2, 4, 9), (6, 8), (1, 5, 7), (2,), (2, 4, 8), (5, 7), (1,
                          # 4, 8), (3, 5, 7), (4,), (3, 8), (1, 8), (1, 4, 9), (6,), (1, 7), (3,
                          # 4), (2, 4)





                          share|improve this answer

























                          • Nice! But don't forget to add singleton solutions (as per strange exception from rule 2)

                            – Drey
                            5 hours ago











                          • Those are in the set, just hard to see :)

                            – hilberts_drinking_problem
                            5 hours ago











                          • Ups, yes, my bad, now I see them :D

                            – Drey
                            5 hours ago













                          2












                          2








                          2







                          Here is a non-recursive solution with a simple for loop. Uniqueness if enforced by applying set to the list of output tuples.



                          lsts = [(1,2,3), (4,5,6), (7,8,9)]

                          res = [[]]
                          for lst in lsts:
                          res += [(*r, x) for r in res for x in lst]

                          # print(tuple(lst) for lst in res[1:])
                          # (5, 9), (4, 7), (6, 9), (1, 4, 7), (2, 6, 9), (4, 8), (3, 4, 7), (2,
                          # 8), (2, 6, 8), (9,), (2, 5, 8), (1, 6), (3, 6, 8), (2, 5, 9), (3, 5,
                          # 9), (3, 7), (2, 5), (3, 6, 9), (5, 8), (1, 6, 8), (3, 5, 8), (2, 6,
                          # 7), (4, 9), (6, 7), (1,), (2, 9), (1, 6, 9), (3,), (1, 5), (5,), (3,
                          # 6), (7,), (3, 6, 7), (1, 5, 9), (2, 6), (2, 4, 7), (1, 5, 8), (3, 4,
                          # 8), (8,), (3, 4, 9), (1, 4), (1, 6, 7), (3, 9), (1, 9), (2, 5, 7), (3,
                          # 5), (2, 7), (2, 4, 9), (6, 8), (1, 5, 7), (2,), (2, 4, 8), (5, 7), (1,
                          # 4, 8), (3, 5, 7), (4,), (3, 8), (1, 8), (1, 4, 9), (6,), (1, 7), (3,
                          # 4), (2, 4)





                          share|improve this answer













                          Here is a non-recursive solution with a simple for loop. Uniqueness if enforced by applying set to the list of output tuples.



                          lsts = [(1,2,3), (4,5,6), (7,8,9)]

                          res = [[]]
                          for lst in lsts:
                          res += [(*r, x) for r in res for x in lst]

                          # print(tuple(lst) for lst in res[1:])
                          # (5, 9), (4, 7), (6, 9), (1, 4, 7), (2, 6, 9), (4, 8), (3, 4, 7), (2,
                          # 8), (2, 6, 8), (9,), (2, 5, 8), (1, 6), (3, 6, 8), (2, 5, 9), (3, 5,
                          # 9), (3, 7), (2, 5), (3, 6, 9), (5, 8), (1, 6, 8), (3, 5, 8), (2, 6,
                          # 7), (4, 9), (6, 7), (1,), (2, 9), (1, 6, 9), (3,), (1, 5), (5,), (3,
                          # 6), (7,), (3, 6, 7), (1, 5, 9), (2, 6), (2, 4, 7), (1, 5, 8), (3, 4,
                          # 8), (8,), (3, 4, 9), (1, 4), (1, 6, 7), (3, 9), (1, 9), (2, 5, 7), (3,
                          # 5), (2, 7), (2, 4, 9), (6, 8), (1, 5, 7), (2,), (2, 4, 8), (5, 7), (1,
                          # 4, 8), (3, 5, 7), (4,), (3, 8), (1, 8), (1, 4, 9), (6,), (1, 7), (3,
                          # 4), (2, 4)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 6 hours ago









                          hilberts_drinking_problemhilberts_drinking_problem

                          6,7713 gold badges14 silver badges32 bronze badges




                          6,7713 gold badges14 silver badges32 bronze badges















                          • Nice! But don't forget to add singleton solutions (as per strange exception from rule 2)

                            – Drey
                            5 hours ago











                          • Those are in the set, just hard to see :)

                            – hilberts_drinking_problem
                            5 hours ago











                          • Ups, yes, my bad, now I see them :D

                            – Drey
                            5 hours ago

















                          • Nice! But don't forget to add singleton solutions (as per strange exception from rule 2)

                            – Drey
                            5 hours ago











                          • Those are in the set, just hard to see :)

                            – hilberts_drinking_problem
                            5 hours ago











                          • Ups, yes, my bad, now I see them :D

                            – Drey
                            5 hours ago
















                          Nice! But don't forget to add singleton solutions (as per strange exception from rule 2)

                          – Drey
                          5 hours ago





                          Nice! But don't forget to add singleton solutions (as per strange exception from rule 2)

                          – Drey
                          5 hours ago













                          Those are in the set, just hard to see :)

                          – hilberts_drinking_problem
                          5 hours ago





                          Those are in the set, just hard to see :)

                          – hilberts_drinking_problem
                          5 hours ago













                          Ups, yes, my bad, now I see them :D

                          – Drey
                          5 hours ago





                          Ups, yes, my bad, now I see them :D

                          – Drey
                          5 hours ago











                          1














                          Another version:



                          from itertools import product

                          lst = [(1,2,3), (4,5,6), (7,8,9)]

                          def generate(lst):
                          for idx in range(len(lst)):
                          for val in lst[idx]:
                          yield (val,)
                          for i in range(len(lst), idx+1, -1):
                          l = tuple((val,) + i for i in product(*lst[idx+1:i]))
                          if len(l) > 1:
                          yield from l

                          l = [*generate(lst)]
                          print(l)


                          Prints:



                          [(1,), (1, 4), (1, 5), (1, 6), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8), (1, 6, 9), (2,), (2, 4), (2, 5), (2, 6), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7), (2, 6, 8), (2, 6, 9), (3,), (3, 4), (3, 5), (3, 6), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 7), (3, 6, 8), (3, 6, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]





                          share|improve this answer































                            1














                            Another version:



                            from itertools import product

                            lst = [(1,2,3), (4,5,6), (7,8,9)]

                            def generate(lst):
                            for idx in range(len(lst)):
                            for val in lst[idx]:
                            yield (val,)
                            for i in range(len(lst), idx+1, -1):
                            l = tuple((val,) + i for i in product(*lst[idx+1:i]))
                            if len(l) > 1:
                            yield from l

                            l = [*generate(lst)]
                            print(l)


                            Prints:



                            [(1,), (1, 4), (1, 5), (1, 6), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8), (1, 6, 9), (2,), (2, 4), (2, 5), (2, 6), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7), (2, 6, 8), (2, 6, 9), (3,), (3, 4), (3, 5), (3, 6), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 7), (3, 6, 8), (3, 6, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]





                            share|improve this answer





























                              1












                              1








                              1







                              Another version:



                              from itertools import product

                              lst = [(1,2,3), (4,5,6), (7,8,9)]

                              def generate(lst):
                              for idx in range(len(lst)):
                              for val in lst[idx]:
                              yield (val,)
                              for i in range(len(lst), idx+1, -1):
                              l = tuple((val,) + i for i in product(*lst[idx+1:i]))
                              if len(l) > 1:
                              yield from l

                              l = [*generate(lst)]
                              print(l)


                              Prints:



                              [(1,), (1, 4), (1, 5), (1, 6), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8), (1, 6, 9), (2,), (2, 4), (2, 5), (2, 6), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7), (2, 6, 8), (2, 6, 9), (3,), (3, 4), (3, 5), (3, 6), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 7), (3, 6, 8), (3, 6, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]





                              share|improve this answer















                              Another version:



                              from itertools import product

                              lst = [(1,2,3), (4,5,6), (7,8,9)]

                              def generate(lst):
                              for idx in range(len(lst)):
                              for val in lst[idx]:
                              yield (val,)
                              for i in range(len(lst), idx+1, -1):
                              l = tuple((val,) + i for i in product(*lst[idx+1:i]))
                              if len(l) > 1:
                              yield from l

                              l = [*generate(lst)]
                              print(l)


                              Prints:



                              [(1,), (1, 4), (1, 5), (1, 6), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8), (1, 6, 9), (2,), (2, 4), (2, 5), (2, 6), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7), (2, 6, 8), (2, 6, 9), (3,), (3, 4), (3, 5), (3, 6), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 7), (3, 6, 8), (3, 6, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited 7 hours ago

























                              answered 8 hours ago









                              Andrej KeselyAndrej Kesely

                              17.9k2 gold badges10 silver badges34 bronze badges




                              17.9k2 gold badges10 silver badges34 bronze badges
























                                  0














                                  Oookay, although I don't fully understand the second rule, here is some short solution to the problem



                                  from itertools import chain, combinations
                                  blubb = [(1,2,3), (4,5,6), (7,8,9)]
                                  blubb_as_set = list(map(set, blubb))

                                  all_blubbs = list(chain.from_iterable(blubb))
                                  all_blubb_combos = (combinations(all_blubbs, i) for i in range(1, 4))
                                  as_a_list = list(chain.from_iterable(all_blubb_combos))

                                  test_subset = lambda x: not any(set(x).issubset(blubb_set) for blubb_set in blubb_as_set)
                                  list(filter(test_subset, as_a_list))
                                  # alternative that includes single elements
                                  list(filter(test_subset, as_a_list)) + list(map(lambda x: (x, ), chain.from_iterable(blubb)))



                                  For the most parts you can leave out list calls.
                                  Your can also create different not_allowed cases based on r if you need to deal with tuple's length more than 3.




                                  Edit: explicit test for subset.






                                  share|improve this answer































                                    0














                                    Oookay, although I don't fully understand the second rule, here is some short solution to the problem



                                    from itertools import chain, combinations
                                    blubb = [(1,2,3), (4,5,6), (7,8,9)]
                                    blubb_as_set = list(map(set, blubb))

                                    all_blubbs = list(chain.from_iterable(blubb))
                                    all_blubb_combos = (combinations(all_blubbs, i) for i in range(1, 4))
                                    as_a_list = list(chain.from_iterable(all_blubb_combos))

                                    test_subset = lambda x: not any(set(x).issubset(blubb_set) for blubb_set in blubb_as_set)
                                    list(filter(test_subset, as_a_list))
                                    # alternative that includes single elements
                                    list(filter(test_subset, as_a_list)) + list(map(lambda x: (x, ), chain.from_iterable(blubb)))



                                    For the most parts you can leave out list calls.
                                    Your can also create different not_allowed cases based on r if you need to deal with tuple's length more than 3.




                                    Edit: explicit test for subset.






                                    share|improve this answer





























                                      0












                                      0








                                      0







                                      Oookay, although I don't fully understand the second rule, here is some short solution to the problem



                                      from itertools import chain, combinations
                                      blubb = [(1,2,3), (4,5,6), (7,8,9)]
                                      blubb_as_set = list(map(set, blubb))

                                      all_blubbs = list(chain.from_iterable(blubb))
                                      all_blubb_combos = (combinations(all_blubbs, i) for i in range(1, 4))
                                      as_a_list = list(chain.from_iterable(all_blubb_combos))

                                      test_subset = lambda x: not any(set(x).issubset(blubb_set) for blubb_set in blubb_as_set)
                                      list(filter(test_subset, as_a_list))
                                      # alternative that includes single elements
                                      list(filter(test_subset, as_a_list)) + list(map(lambda x: (x, ), chain.from_iterable(blubb)))



                                      For the most parts you can leave out list calls.
                                      Your can also create different not_allowed cases based on r if you need to deal with tuple's length more than 3.




                                      Edit: explicit test for subset.






                                      share|improve this answer















                                      Oookay, although I don't fully understand the second rule, here is some short solution to the problem



                                      from itertools import chain, combinations
                                      blubb = [(1,2,3), (4,5,6), (7,8,9)]
                                      blubb_as_set = list(map(set, blubb))

                                      all_blubbs = list(chain.from_iterable(blubb))
                                      all_blubb_combos = (combinations(all_blubbs, i) for i in range(1, 4))
                                      as_a_list = list(chain.from_iterable(all_blubb_combos))

                                      test_subset = lambda x: not any(set(x).issubset(blubb_set) for blubb_set in blubb_as_set)
                                      list(filter(test_subset, as_a_list))
                                      # alternative that includes single elements
                                      list(filter(test_subset, as_a_list)) + list(map(lambda x: (x, ), chain.from_iterable(blubb)))



                                      For the most parts you can leave out list calls.
                                      Your can also create different not_allowed cases based on r if you need to deal with tuple's length more than 3.




                                      Edit: explicit test for subset.







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited 7 hours ago

























                                      answered 7 hours ago









                                      DreyDrey

                                      1,97714 silver badges18 bronze badges




                                      1,97714 silver badges18 bronze badges






























                                          draft saved

                                          draft discarded
















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57444657%2funique-combinations-of-a-list-of-tuples%23new-answer', 'question_page');

                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          ParseJSON using SSJSUsing AMPscript with SSJS ActivitiesHow to resubscribe a user in Marketing cloud using SSJS?Pulling Subscriber Status from Lists using SSJSRetrieving Emails using SSJSProblem in updating DE using SSJSUsing SSJS to send single email in Marketing CloudError adding EmailSendDefinition using SSJS

                                          Кампала Садржај Географија Географија Историја Становништво Привреда Партнерски градови Референце Спољашње везе Мени за навигацију0°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.340°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.34МедијиПодациЗванични веб-сајту

                                          19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу