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;
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
add a comment |
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
4
itertools.productwill 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
add a comment |
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
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
python tuples
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.productwill 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
add a comment |
4
itertools.productwill 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
add a comment |
6 Answers
6
active
oldest
votes
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.
1
sport == code golf here?
– val
Aug 12 at 9:23
add a comment |
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.
add a comment |
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.)
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
add a comment |
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.
add a comment |
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)))])
Downvoted because this explicitly relies on botht1andt2having length 2, and will produce incorrect output otherwise.
– donkopotamus
yesterday
add a comment |
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))
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
1
sport == code golf here?
– val
Aug 12 at 9:23
add a comment |
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.
1
sport == code golf here?
– val
Aug 12 at 9:23
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
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
add a comment |
add a comment |
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.)
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
add a comment |
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.)
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
add a comment |
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.)
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.)
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Aug 11 at 20:47
donkopotamusdonkopotamus
13.9k1 gold badge28 silver badges42 bronze badges
13.9k1 gold badge28 silver badges42 bronze badges
add a comment |
add a comment |
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)))])
Downvoted because this explicitly relies on botht1andt2having length 2, and will produce incorrect output otherwise.
– donkopotamus
yesterday
add a comment |
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)))])
Downvoted because this explicitly relies on botht1andt2having length 2, and will produce incorrect output otherwise.
– donkopotamus
yesterday
add a comment |
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)))])
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)))])
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 botht1andt2having length 2, and will produce incorrect output otherwise.
– donkopotamus
yesterday
add a comment |
Downvoted because this explicitly relies on botht1andt2having 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
add a comment |
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))
add a comment |
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))
add a comment |
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))
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))
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
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
4
itertools.productwill 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