create a tuple from pairsWhat is the equivalent of the C++ Pair<L,R> in Java?How can I safely create a nested directory?What's the difference between lists and tuples?What are “named tuples” in Python?Is Using .NET 4.0 Tuples in my C# Code a Poor Design Decision?How to sort (list/tuple) of lists/tuples by the element at a given index?Convert list to tuple in PythonChoose two tuples from a list and calculate every possible pair in Python

Does travel insurance for short flight delays exist?

Why do all fields in a QFT transform like *irreducible* representations of some group?

Is "The life is beautiful" incorrect or just very non-idiomatic?

Confirming resignation after resignation letter ripped up

Identify this sanskrit mantra?

Handling Disruptive Student on the Autistic Spectrum

Would it be possible to have a GMO that produces chocolate?

Pythagorean triple with hypotenuse a power of 2

How do I request a longer than normal leave of absence period for my wedding?

Would this system work to purify water?

Why were the crew so desperate to catch Truman and return him to Seahaven?

French abbreviation for comparing two items ("vs")

Sci fi film similar to Village of the Damned

How would one country purchase another?

Shouldn't the "credit score" prevent Americans from going deeper and deeper into personal debt?

Was it ever possible to target a zone?

How do you harvest carrots in creative mode?

Why does The Ancient One think differently about Doctor Strange in Endgame than the film Doctor Strange?

Couple of slangs I've heard when watching anime

How to find out the average duration of the peer-review process for a given journal?

Dealing with an extrovert co-worker

Understanding Parallelize methods

Why isn't "I've" a proper response?

Is there any method of inflicting the incapacitated condition and no other condition?



create a tuple from pairs


What is the equivalent of the C++ Pair<L,R> in Java?How can I safely create a nested directory?What's the difference between lists and tuples?What are “named tuples” in Python?Is Using .NET 4.0 Tuples in my C# Code a Poor Design Decision?How to sort (list/tuple) of lists/tuples by the element at a given index?Convert list to tuple in PythonChoose two tuples from a list and calculate every possible pair in Python






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








13















I would like to create a tuple which present all the possible pairs from two tuples



this is example for what I would like to receive :



first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)


output :



((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))


This is what I did which succeed however look a bit cumbersome :



def mult_tuple(tuple1, tuple2):
ls=[]
for t1 in tuple1:

for t2 in tuple2:
c=(t1,t2)
d=(t2,t1)
ls.append(c)
ls.append(d)

return tuple(ls)


first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)


The code I wrote works , however I am looking for a nicer code

thank you in advance










share|improve this question





















  • 4





    itertools.product will get you half-way there.

    – DeepSpace
    Aug 11 at 20:38






  • 2





    You might also want to consider if you’re looking for unique tuples. Would you want to add both (1, 1) and it’s reverse?

    – donkopotamus
    Aug 11 at 20:40






  • 2





    I think this question would have been better suited for Code Review SE

    – Michael A. Schaffrath
    Aug 12 at 13:03

















13















I would like to create a tuple which present all the possible pairs from two tuples



this is example for what I would like to receive :



first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)


output :



((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))


This is what I did which succeed however look a bit cumbersome :



def mult_tuple(tuple1, tuple2):
ls=[]
for t1 in tuple1:

for t2 in tuple2:
c=(t1,t2)
d=(t2,t1)
ls.append(c)
ls.append(d)

return tuple(ls)


first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)


The code I wrote works , however I am looking for a nicer code

thank you in advance










share|improve this question





















  • 4





    itertools.product will get you half-way there.

    – DeepSpace
    Aug 11 at 20:38






  • 2





    You might also want to consider if you’re looking for unique tuples. Would you want to add both (1, 1) and it’s reverse?

    – donkopotamus
    Aug 11 at 20:40






  • 2





    I think this question would have been better suited for Code Review SE

    – Michael A. Schaffrath
    Aug 12 at 13:03













13












13








13


1






I would like to create a tuple which present all the possible pairs from two tuples



this is example for what I would like to receive :



first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)


output :



((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))


This is what I did which succeed however look a bit cumbersome :



def mult_tuple(tuple1, tuple2):
ls=[]
for t1 in tuple1:

for t2 in tuple2:
c=(t1,t2)
d=(t2,t1)
ls.append(c)
ls.append(d)

return tuple(ls)


first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)


The code I wrote works , however I am looking for a nicer code

thank you in advance










share|improve this question
















I would like to create a tuple which present all the possible pairs from two tuples



this is example for what I would like to receive :



first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)


output :



((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))


This is what I did which succeed however look a bit cumbersome :



def mult_tuple(tuple1, tuple2):
ls=[]
for t1 in tuple1:

for t2 in tuple2:
c=(t1,t2)
d=(t2,t1)
ls.append(c)
ls.append(d)

return tuple(ls)


first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)


The code I wrote works , however I am looking for a nicer code

thank you in advance







python tuples






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 11 at 20:35









MrGeek

9,8872 gold badges15 silver badges37 bronze badges




9,8872 gold badges15 silver badges37 bronze badges










asked Aug 11 at 20:34









zachizachi

765 bronze badges




765 bronze badges










  • 4





    itertools.product will get you half-way there.

    – DeepSpace
    Aug 11 at 20:38






  • 2





    You might also want to consider if you’re looking for unique tuples. Would you want to add both (1, 1) and it’s reverse?

    – donkopotamus
    Aug 11 at 20:40






  • 2





    I think this question would have been better suited for Code Review SE

    – Michael A. Schaffrath
    Aug 12 at 13:03












  • 4





    itertools.product will get you half-way there.

    – DeepSpace
    Aug 11 at 20:38






  • 2





    You might also want to consider if you’re looking for unique tuples. Would you want to add both (1, 1) and it’s reverse?

    – donkopotamus
    Aug 11 at 20:40






  • 2





    I think this question would have been better suited for Code Review SE

    – Michael A. Schaffrath
    Aug 12 at 13:03







4




4





itertools.product will get you half-way there.

– DeepSpace
Aug 11 at 20:38





itertools.product will get you half-way there.

– DeepSpace
Aug 11 at 20:38




2




2





You might also want to consider if you’re looking for unique tuples. Would you want to add both (1, 1) and it’s reverse?

– donkopotamus
Aug 11 at 20:40





You might also want to consider if you’re looking for unique tuples. Would you want to add both (1, 1) and it’s reverse?

– donkopotamus
Aug 11 at 20:40




2




2





I think this question would have been better suited for Code Review SE

– Michael A. Schaffrath
Aug 12 at 13:03





I think this question would have been better suited for Code Review SE

– Michael A. Schaffrath
Aug 12 at 13:03












6 Answers
6






active

oldest

votes


















6















Here is an ugly one-liner.



first_tuple = (1, 2)
second_tuple = (4, 5)
tups = [first_tuple, second_tuple]
res = [(i, j) for x in tups for y in tups for i in x for j in y if x is not y]
# [(1, 4), (1, 5), (2, 4), (2, 5), (4, 1), (4, 2), (5, 1), (5, 2)]


Unless you are using this for sport, you should probably go with a more readable solution, e.g. one by MrGeek below.






share|improve this answer






















  • 1





    sport == code golf here?

    – val
    Aug 12 at 9:23


















31















You can use itertools's product and permutations:



from itertools import product, permutations

ls = []

for tup in product(first_tuple, second_tuple):
ls.extend(permutations(tup))

print(ls)


Output:



[(1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2)]


product produces the tuples (two elements) produced equally by the nested for loop structure (your t1 and t2 variables), and permutations produces the two permutations produced equally by your c and d variables, extend is used here instead of two appends.






share|improve this answer


































    6















    itertools.product gives you what you want. However, since the Cartesian product of two tuples is not commutative (product(x,y) != product(y,x)), you need to compute both and concatenate the results.



    >>> from itertools import chain, product
    >>> x = (1,4)
    >>> y = (2, 5)
    >>> list(chain(product(x,y), product(y,x)))
    [(1, 2), (1, 5), (4, 2), (4, 5), (2, 1), (2, 4), (5, 1), (5, 4)]


    (You can use chain here instead of permutations because there are only two permutations of a 2-tuple, which are easy enough to specify explicitly.)






    share|improve this answer






















    • 1





      You can also unpack iterables to produce the end list in an arguably cleaner way: [*product(x, y), *product(y, x)]

      – Sławomir Górawski
      Aug 12 at 12:49



















    4















    If you’d like to avoid the use of the standard library (itertools) then simply combine two list comprehensions:



    result = [(x, y) for x in first_tuple for y in second_tuple]
    result.extend( (x, y) for x in second_tuple for y in first_tuple )


    then convert to a tuple if it’s important to you.






    share|improve this answer
































      1















      Also You can do:



      from itertools import permutations 
      t1=(1,2)
      t2=(3,4)
      my_tuple=tuple([key for key in filter(lambda x: x!=t1 and (x!=t2),list(permutations(t1+t2,2)))])





      share|improve this answer



























      • Downvoted because this explicitly relies on both t1 and t2 having length 2, and will produce incorrect output otherwise.

        – donkopotamus
        yesterday


















      1















      first_tuple = (1, 2)
      second_tuple = (4, 5)

      out = []
      for val in first_tuple:
      for val2 in second_tuple:
      out.append((val, val2))
      out.append((val2, val))

      print(tuple(out))


      Prints:



      ((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))





      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%2f57453407%2fcreate-a-tuple-from-pairs%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        6 Answers
        6






        active

        oldest

        votes








        6 Answers
        6






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        6















        Here is an ugly one-liner.



        first_tuple = (1, 2)
        second_tuple = (4, 5)
        tups = [first_tuple, second_tuple]
        res = [(i, j) for x in tups for y in tups for i in x for j in y if x is not y]
        # [(1, 4), (1, 5), (2, 4), (2, 5), (4, 1), (4, 2), (5, 1), (5, 2)]


        Unless you are using this for sport, you should probably go with a more readable solution, e.g. one by MrGeek below.






        share|improve this answer






















        • 1





          sport == code golf here?

          – val
          Aug 12 at 9:23















        6















        Here is an ugly one-liner.



        first_tuple = (1, 2)
        second_tuple = (4, 5)
        tups = [first_tuple, second_tuple]
        res = [(i, j) for x in tups for y in tups for i in x for j in y if x is not y]
        # [(1, 4), (1, 5), (2, 4), (2, 5), (4, 1), (4, 2), (5, 1), (5, 2)]


        Unless you are using this for sport, you should probably go with a more readable solution, e.g. one by MrGeek below.






        share|improve this answer






















        • 1





          sport == code golf here?

          – val
          Aug 12 at 9:23













        6














        6










        6









        Here is an ugly one-liner.



        first_tuple = (1, 2)
        second_tuple = (4, 5)
        tups = [first_tuple, second_tuple]
        res = [(i, j) for x in tups for y in tups for i in x for j in y if x is not y]
        # [(1, 4), (1, 5), (2, 4), (2, 5), (4, 1), (4, 2), (5, 1), (5, 2)]


        Unless you are using this for sport, you should probably go with a more readable solution, e.g. one by MrGeek below.






        share|improve this answer















        Here is an ugly one-liner.



        first_tuple = (1, 2)
        second_tuple = (4, 5)
        tups = [first_tuple, second_tuple]
        res = [(i, j) for x in tups for y in tups for i in x for j in y if x is not y]
        # [(1, 4), (1, 5), (2, 4), (2, 5), (4, 1), (4, 2), (5, 1), (5, 2)]


        Unless you are using this for sport, you should probably go with a more readable solution, e.g. one by MrGeek below.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 12 at 6:04

























        answered Aug 11 at 21:17









        hilberts_drinking_problemhilberts_drinking_problem

        7,0893 gold badges14 silver badges35 bronze badges




        7,0893 gold badges14 silver badges35 bronze badges










        • 1





          sport == code golf here?

          – val
          Aug 12 at 9:23












        • 1





          sport == code golf here?

          – val
          Aug 12 at 9:23







        1




        1





        sport == code golf here?

        – val
        Aug 12 at 9:23





        sport == code golf here?

        – val
        Aug 12 at 9:23













        31















        You can use itertools's product and permutations:



        from itertools import product, permutations

        ls = []

        for tup in product(first_tuple, second_tuple):
        ls.extend(permutations(tup))

        print(ls)


        Output:



        [(1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2)]


        product produces the tuples (two elements) produced equally by the nested for loop structure (your t1 and t2 variables), and permutations produces the two permutations produced equally by your c and d variables, extend is used here instead of two appends.






        share|improve this answer































          31















          You can use itertools's product and permutations:



          from itertools import product, permutations

          ls = []

          for tup in product(first_tuple, second_tuple):
          ls.extend(permutations(tup))

          print(ls)


          Output:



          [(1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2)]


          product produces the tuples (two elements) produced equally by the nested for loop structure (your t1 and t2 variables), and permutations produces the two permutations produced equally by your c and d variables, extend is used here instead of two appends.






          share|improve this answer





























            31














            31










            31









            You can use itertools's product and permutations:



            from itertools import product, permutations

            ls = []

            for tup in product(first_tuple, second_tuple):
            ls.extend(permutations(tup))

            print(ls)


            Output:



            [(1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2)]


            product produces the tuples (two elements) produced equally by the nested for loop structure (your t1 and t2 variables), and permutations produces the two permutations produced equally by your c and d variables, extend is used here instead of two appends.






            share|improve this answer















            You can use itertools's product and permutations:



            from itertools import product, permutations

            ls = []

            for tup in product(first_tuple, second_tuple):
            ls.extend(permutations(tup))

            print(ls)


            Output:



            [(1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2)]


            product produces the tuples (two elements) produced equally by the nested for loop structure (your t1 and t2 variables), and permutations produces the two permutations produced equally by your c and d variables, extend is used here instead of two appends.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 13 at 20:10

























            answered Aug 11 at 20:41









            MrGeekMrGeek

            9,8872 gold badges15 silver badges37 bronze badges




            9,8872 gold badges15 silver badges37 bronze badges
























                6















                itertools.product gives you what you want. However, since the Cartesian product of two tuples is not commutative (product(x,y) != product(y,x)), you need to compute both and concatenate the results.



                >>> from itertools import chain, product
                >>> x = (1,4)
                >>> y = (2, 5)
                >>> list(chain(product(x,y), product(y,x)))
                [(1, 2), (1, 5), (4, 2), (4, 5), (2, 1), (2, 4), (5, 1), (5, 4)]


                (You can use chain here instead of permutations because there are only two permutations of a 2-tuple, which are easy enough to specify explicitly.)






                share|improve this answer






















                • 1





                  You can also unpack iterables to produce the end list in an arguably cleaner way: [*product(x, y), *product(y, x)]

                  – Sławomir Górawski
                  Aug 12 at 12:49
















                6















                itertools.product gives you what you want. However, since the Cartesian product of two tuples is not commutative (product(x,y) != product(y,x)), you need to compute both and concatenate the results.



                >>> from itertools import chain, product
                >>> x = (1,4)
                >>> y = (2, 5)
                >>> list(chain(product(x,y), product(y,x)))
                [(1, 2), (1, 5), (4, 2), (4, 5), (2, 1), (2, 4), (5, 1), (5, 4)]


                (You can use chain here instead of permutations because there are only two permutations of a 2-tuple, which are easy enough to specify explicitly.)






                share|improve this answer






















                • 1





                  You can also unpack iterables to produce the end list in an arguably cleaner way: [*product(x, y), *product(y, x)]

                  – Sławomir Górawski
                  Aug 12 at 12:49














                6














                6










                6









                itertools.product gives you what you want. However, since the Cartesian product of two tuples is not commutative (product(x,y) != product(y,x)), you need to compute both and concatenate the results.



                >>> from itertools import chain, product
                >>> x = (1,4)
                >>> y = (2, 5)
                >>> list(chain(product(x,y), product(y,x)))
                [(1, 2), (1, 5), (4, 2), (4, 5), (2, 1), (2, 4), (5, 1), (5, 4)]


                (You can use chain here instead of permutations because there are only two permutations of a 2-tuple, which are easy enough to specify explicitly.)






                share|improve this answer















                itertools.product gives you what you want. However, since the Cartesian product of two tuples is not commutative (product(x,y) != product(y,x)), you need to compute both and concatenate the results.



                >>> from itertools import chain, product
                >>> x = (1,4)
                >>> y = (2, 5)
                >>> list(chain(product(x,y), product(y,x)))
                [(1, 2), (1, 5), (4, 2), (4, 5), (2, 1), (2, 4), (5, 1), (5, 4)]


                (You can use chain here instead of permutations because there are only two permutations of a 2-tuple, which are easy enough to specify explicitly.)







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 11 at 21:37

























                answered Aug 11 at 21:22









                chepnerchepner

                284k40 gold badges280 silver badges379 bronze badges




                284k40 gold badges280 silver badges379 bronze badges










                • 1





                  You can also unpack iterables to produce the end list in an arguably cleaner way: [*product(x, y), *product(y, x)]

                  – Sławomir Górawski
                  Aug 12 at 12:49













                • 1





                  You can also unpack iterables to produce the end list in an arguably cleaner way: [*product(x, y), *product(y, x)]

                  – Sławomir Górawski
                  Aug 12 at 12:49








                1




                1





                You can also unpack iterables to produce the end list in an arguably cleaner way: [*product(x, y), *product(y, x)]

                – Sławomir Górawski
                Aug 12 at 12:49






                You can also unpack iterables to produce the end list in an arguably cleaner way: [*product(x, y), *product(y, x)]

                – Sławomir Górawski
                Aug 12 at 12:49












                4















                If you’d like to avoid the use of the standard library (itertools) then simply combine two list comprehensions:



                result = [(x, y) for x in first_tuple for y in second_tuple]
                result.extend( (x, y) for x in second_tuple for y in first_tuple )


                then convert to a tuple if it’s important to you.






                share|improve this answer





























                  4















                  If you’d like to avoid the use of the standard library (itertools) then simply combine two list comprehensions:



                  result = [(x, y) for x in first_tuple for y in second_tuple]
                  result.extend( (x, y) for x in second_tuple for y in first_tuple )


                  then convert to a tuple if it’s important to you.






                  share|improve this answer



























                    4














                    4










                    4









                    If you’d like to avoid the use of the standard library (itertools) then simply combine two list comprehensions:



                    result = [(x, y) for x in first_tuple for y in second_tuple]
                    result.extend( (x, y) for x in second_tuple for y in first_tuple )


                    then convert to a tuple if it’s important to you.






                    share|improve this answer













                    If you’d like to avoid the use of the standard library (itertools) then simply combine two list comprehensions:



                    result = [(x, y) for x in first_tuple for y in second_tuple]
                    result.extend( (x, y) for x in second_tuple for y in first_tuple )


                    then convert to a tuple if it’s important to you.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 11 at 20:47









                    donkopotamusdonkopotamus

                    13.9k1 gold badge28 silver badges42 bronze badges




                    13.9k1 gold badge28 silver badges42 bronze badges
























                        1















                        Also You can do:



                        from itertools import permutations 
                        t1=(1,2)
                        t2=(3,4)
                        my_tuple=tuple([key for key in filter(lambda x: x!=t1 and (x!=t2),list(permutations(t1+t2,2)))])





                        share|improve this answer



























                        • Downvoted because this explicitly relies on both t1 and t2 having length 2, and will produce incorrect output otherwise.

                          – donkopotamus
                          yesterday















                        1















                        Also You can do:



                        from itertools import permutations 
                        t1=(1,2)
                        t2=(3,4)
                        my_tuple=tuple([key for key in filter(lambda x: x!=t1 and (x!=t2),list(permutations(t1+t2,2)))])





                        share|improve this answer



























                        • Downvoted because this explicitly relies on both t1 and t2 having length 2, and will produce incorrect output otherwise.

                          – donkopotamus
                          yesterday













                        1














                        1










                        1









                        Also You can do:



                        from itertools import permutations 
                        t1=(1,2)
                        t2=(3,4)
                        my_tuple=tuple([key for key in filter(lambda x: x!=t1 and (x!=t2),list(permutations(t1+t2,2)))])





                        share|improve this answer















                        Also You can do:



                        from itertools import permutations 
                        t1=(1,2)
                        t2=(3,4)
                        my_tuple=tuple([key for key in filter(lambda x: x!=t1 and (x!=t2),list(permutations(t1+t2,2)))])






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Aug 11 at 21:51

























                        answered Aug 11 at 21:06









                        lostCodelostCode

                        69811 bronze badges




                        69811 bronze badges















                        • Downvoted because this explicitly relies on both t1 and t2 having length 2, and will produce incorrect output otherwise.

                          – donkopotamus
                          yesterday

















                        • Downvoted because this explicitly relies on both t1 and t2 having length 2, and will produce incorrect output otherwise.

                          – donkopotamus
                          yesterday
















                        Downvoted because this explicitly relies on both t1 and t2 having length 2, and will produce incorrect output otherwise.

                        – donkopotamus
                        yesterday





                        Downvoted because this explicitly relies on both t1 and t2 having length 2, and will produce incorrect output otherwise.

                        – donkopotamus
                        yesterday











                        1















                        first_tuple = (1, 2)
                        second_tuple = (4, 5)

                        out = []
                        for val in first_tuple:
                        for val2 in second_tuple:
                        out.append((val, val2))
                        out.append((val2, val))

                        print(tuple(out))


                        Prints:



                        ((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))





                        share|improve this answer































                          1















                          first_tuple = (1, 2)
                          second_tuple = (4, 5)

                          out = []
                          for val in first_tuple:
                          for val2 in second_tuple:
                          out.append((val, val2))
                          out.append((val2, val))

                          print(tuple(out))


                          Prints:



                          ((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))





                          share|improve this answer





























                            1














                            1










                            1









                            first_tuple = (1, 2)
                            second_tuple = (4, 5)

                            out = []
                            for val in first_tuple:
                            for val2 in second_tuple:
                            out.append((val, val2))
                            out.append((val2, val))

                            print(tuple(out))


                            Prints:



                            ((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))





                            share|improve this answer















                            first_tuple = (1, 2)
                            second_tuple = (4, 5)

                            out = []
                            for val in first_tuple:
                            for val2 in second_tuple:
                            out.append((val, val2))
                            out.append((val2, val))

                            print(tuple(out))


                            Prints:



                            ((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Aug 11 at 21:59

























                            answered Aug 11 at 21:50









                            Andrej KeselyAndrej Kesely

                            20.2k3 gold badges12 silver badges36 bronze badges




                            20.2k3 gold badges12 silver badges36 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%2f57453407%2fcreate-a-tuple-from-pairs%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

                                Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

                                Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

                                Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림