# Python | Mutual tuple subtraction in list

Sometimes, while working with data, we can have a problem in which we need to perform tuple subtraction among all the tuples in list. This can have application in many domains. Let’s discuss certain ways in which this task can be performed.

**Method #1 : Using combinations() + list comprehension**

This problem can be solved using combinations of above functions. In this, we use combinations() to generate all possible combination among tuples and list comprehension is used to feed subtraction logic.

`# Python3 code to demonstrate working of` `# Mutual tuple subtraction in list` `# Using list comprehension + combinations` `from` `itertools ` `import` `combinations` ` ` `# initialize list ` `test_list ` `=` `[(` `2` `, ` `4` `), (` `6` `, ` `7` `), (` `5` `, ` `1` `), (` `6` `, ` `10` `)]` ` ` `# printing original list ` `print` `(` `"The original list : "` `+` `str` `(test_list))` ` ` `# Mutual tuple subtraction in list` `# Using list comprehension + combinations` `res ` `=` `[(b1 ` `-` `a1, b2 ` `-` `a2) ` `for` `(a1, a2), (b1, b2) ` `in` `combinations(test_list, ` `2` `)] ` ` ` `# printing result` `print` `(` `"The mutual subtraction tuples are : "` `+` `str` `(res))` |

**Output :**

The original list : [(2, 4), (6, 7), (5, 1), (6, 10)] The mutual subtraction tuples are : [(4, 3), (3, -3), (4, 6), (-1, -6), (0, 3), (1, 9)]

**Method #2 : Using list comprehension + zip() + operator.sub + combinations()**

The combinations of above methods can also solve this problem. In this, we perform the task of subtraction using sub() and the like indexed elements are linked using zip().

`# Python3 code to demonstrate working of` `# Mutual tuple subtraction in list` `# Using list comprehension + zip() + operator.sub + combinations()` `from` `itertools ` `import` `combinations` `import` `operator` ` ` `# initialize list ` `test_list ` `=` `[(` `2` `, ` `4` `), (` `6` `, ` `7` `), (` `5` `, ` `1` `), (` `6` `, ` `10` `)]` ` ` `# printing original list ` `print` `(` `"The original list : "` `+` `str` `(test_list))` ` ` `# Mutual tuple subtraction in list` `# Using list comprehension + zip() + operator.sub + combinations()` `res ` `=` `[(operator.sub(` `*` `a), operator.sub(` `*` `b))\` ` ` `for` `a, b ` `in` `(` `zip` `(y, x) ` `for` `x, y ` `in` `combinations(test_list, ` `2` `))]` ` ` `# printing result` `print` `(` `"The mutual subtraction tuples are : "` `+` `str` `(res))` |

**Output :**

The original list : [(2, 4), (6, 7), (5, 1), (6, 10)] The mutual subtraction tuples are : [(4, 3), (3, -3), (4, 6), (-1, -6), (0, 3), (1, 9)]

Attention geek! Strengthen your foundations with the **Python Programming Foundation** Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the **Python DS** Course. And to begin with your Machine Learning Journey, join the **Machine Learning – Basic Level Course**