# Python – Check if all tuples have element difference less than K

• Last Updated : 15 Mar, 2023

Given a Tuple list, check if each tuple has a difference less than K.

Input : test_list = [(3, 4), (1, 2), (7, 8), (9, 13)], K = 2
Output : False
Explanation : 13 – 9 = 4 > 2.

Input : test_list = [(3, 4), (1, 2), (7, 8)], K = 2
Output : True
Explanation : All have abs. diff 1 < 2.

Method #1 : Using loop

In this, we keep a boolean variable and check if any element is greater than or equal to K, then mark it False and break.

## Python3

 `# Python3 code to demonstrate working of``# Check if all tuples have element difference less than K``# Using loop` `# initializing list``test_list ``=` `[(``3``, ``4``), (``1``, ``2``), (``7``, ``8``), (``9``, ``8``)]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing K``K ``=` `2` `res ``=` `True``for` `ele1, ele2 ``in` `test_list:``    ` `    ``# using abs() to compute absolute difference``    ``if` `abs``(ele1 ``-` `ele2) >``=` `K:``        ``res ``=` `False` `# printing result``print``(``"Are all elements difference less than K ? : "` `+` `str``(res))`

Output

```The original list is : [(3, 4), (1, 2), (7, 8), (9, 8)]
Are all elements difference less than K ? : True```

Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(1), as we are not using any extra data structures that depend on the size of the input.

Method #2 : Using all()

In this, we use all() to check if all the tuples have difference within K.

## Python3

 `# Python3 code to demonstrate working of``# Check if all tuples have element difference less than K``# Using all()` `# initializing list``test_list ``=` `[(``3``, ``4``), (``1``, ``2``), (``7``, ``8``), (``9``, ``8``)]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing K``K ``=` `2` `# shorthand to solve this problem``res ``=` `all``(``abs``(sub1 ``-` `sub2) < K ``for` `sub1, sub2 ``in` `test_list)` `# printing result``print``(``"Are all elements difference less than K ? : "` `+` `str``(res))`

Output

```The original list is : [(3, 4), (1, 2), (7, 8), (9, 8)]
Are all elements difference less than K ? : True```

Time complexity: O(n), where n is the number of tuples in the list.

Auxiliary space: O(1), as only constant extra space is used to store the value of K and the result.

Method#3: Using Recursive method.

## Python3

 `# Python3 code to demonstrate working of``# Check if all tuples have element difference less than K``# Using recursive function``def` `is_lessthanK(lst,K,start``=``0``):``  ``if` `start``=``=``len``(lst):``#base condition``    ``return` `True``  ``if` `abs``(lst[start][``0``] ``-` `lst[start][``1``]) > K:``    ``return` `False``  ``return` `is_lessthanK(lst,K,start``+``1``)` `# initializing list``test_list ``=` `[(``3``, ``4``), (``1``, ``2``), (``7``, ``8``), (``9``, ``8``)]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing K``K ``=` `2` `res ``=` `is_lessthanK(test_list,K)` `# printing result``print``(``"Are all elements difference less than K ? : "` `+` `str``(res))``#this code contributed by tvsk`

Output

```The original list is : [(3, 4), (1, 2), (7, 8), (9, 8)]
Are all elements difference less than K ? : True```

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4 using combinations

#### Approach

Using combinations from the itertools module to generate all possible pairs of tuples.

#### Algorithm

1. Import the combinations function from the itertools module.
2. Generate all possible pairs of tuples using the combinations function.
3. Iterate through each pair of tuples and calculate the absolute difference between each pair of elements in the two tuples.
4. If the absolute difference is greater than or equal to K, return False.
5. If all pairs have absolute differences less than K, return True.

## Python3

 `from` `itertools ``import` `combinations` `def` `check_tuple_difference(test_list, K):``    ``for` `i, j ``in` `combinations(test_list, ``2``):``        ``diff ``=` `abs``(i[``0``]``-``j[``0``]) ``+` `abs``(i[``1``]``-``j[``1``])``        ``if` `diff >``=` `K:``            ``return` `False``    ``return` `True` `test_list ``=` `[(``3``, ``4``), (``1``, ``2``), (``7``, ``8``), (``9``, ``13``)]``K ``=` `2``print``(check_tuple_difference(test_list, K))`

Output

`False`

Time complexity:  O(n^2) because we generate all possible pairs of tuples.
Auxiliary Space: O(1)

Method #5: Using any() and filter() function

Step by step approach:

1. Initialize the test_list and K.
2. Use the filter() function to filter out the tuples from the test_list that have an element difference greater than or equal to K.
3. Check if any tuples are left in the filtered result using the any() function.
4. Negate the result using the not operator to get the final result.
5. Print the final result.

## Python3

 `# initializing list``test_list ``=` `[(``3``, ``4``), (``1``, ``2``), (``7``, ``8``), (``9``, ``8``)]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing K``K ``=` `2` `res ``=` `not` `any``(``filter``(``lambda` `x: ``abs``(x[``0``]``-``x[``1``])>``=``K, test_list))` `# printing result``print``(``"Are all elements difference less than K ? : "` `+` `str``(res))``#This code is contributed by Vinay Pinjala.`

Output

```The original list is : [(3, 4), (1, 2), (7, 8), (9, 8)]
Are all elements difference less than K ? : True```

Time Complexity: O(n), where n is the length of test_list. The filter() function iterates over each tuple in the list once to check the element difference, and the any() function also iterates over the filtered result to check if any tuples are left.

Auxiliary Space: O(1), because we use only a single boolean variable to store the result. The filter() function and lambda function use negligible space, and the any() function does not create any additional data structure.

My Personal Notes arrow_drop_up