Python | Checking triangular inequality on list of lists Last Updated : 18 Apr, 2023 Comments Improve Suggest changes Like Article Like Report Given a list of lists, the task is to find whether a sublist satisfies the triangle inequality. The triangle inequality states that for any triangle, the sum of the lengths of any two sides must be greater than or equal to the length of the remaining side. In other words, a triangle is valid if sum of its two sides is greater than the third side. If three sides are a, b and c, then three conditions should be met. a + b > c a + c > b b + c > a Method #1 : Using List comprehension Python3 # Python code to find whether a sublist # satisfies the triangle inequality. # List initialization Input = [[1, 3, 1], [4, 5, 6]] # Sorting sublist for elem in Input: elem.sort() # Using list comprehension Output = [(p, q, r) for p, q, r in Input if (p + q)>= r] # Printing output print(Output) Output:[(4, 5, 6)] Time Complexity: O(n), where n is the length of the list test_list Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list Method #2 : Using Iteration Python3 # Python code to find whether a sublist # satisfies the triangle inequality. # List initialization Input = [[1, 1, 3], [4, 5, 6]] # Sorting sublist of list of list for elem in Input: elem.sort() # Checking for triangular inequality for elem in Input: if elem[0] + elem[1] > elem[2]: print(elem) Output:[4, 5, 6] Method #3 : Using filter Another approach you can use is to use the filter function to filter the sublists in the input list that satisfy the triangle inequality. Here is an example of how you can do this: Python3 # List initialization Input = [[1, 1, 3], [4, 5, 6]] # Check if the triangle inequality holds for each sublist valid_sublists = filter(lambda x: x[0] + x[1] > x[2] and x[0] + x[2] > x[1] and x[1] + x[2] > x[0], Input) print(list(valid_sublists)) #This code is contributed by Edula Vinay Kumar Reddy Output[[4, 5, 6]] The time complexity of this approach is O(n), where n is the number of sublists in the input list, because the filter function iterates over each element in the input list and applies the filtering function to it. The space complexity is also O(n), because the valid_sublists list will have a size equal to the number of sublists that satisfy the triangle inequality. Comment More infoAdvertise with us Next Article Python | Checking triangular inequality on list of lists everythingispossible Follow Improve Article Tags : Python Python Programs Python list-programs Practice Tags : python Similar Reads Check if List is Strictly Increasing - Python We are given a list of numbers and our task is to check whether the list is strictly increasing meaning each element must be greater than the previous one. For example: a = [1, 3, 5, 7] this is strictly increasing and b = [1, 3, 3, 7], this is not strictly increasing as 3 appears twice.Using all() w 2 min read Python | Check if a list exists in given list of lists Given a list of lists, the task is to check if a list exists in given list of lists. Input : lst = [[1, 1, 1, 2], [2, 3, 4], [1, 2, 3], [4, 5, 6]] list_search = [4, 5, 6] Output: True Input : lst = [[5, 6, 7], [12, 54, 9], [1, 2, 3]] list_search = [4, 12, 54] Output: False Letâs discuss certain ways 4 min read Sorting List of Lists with First Element of Each Sub-List in Python In Python, sorting a list of lists by the first element of each sub-list is a common task. Whether you're dealing with data points, coordinates, or any other structured information, arranging the lists based on the values of their first elements can be crucial. In this article, we will sort a list o 3 min read Python Program to check if matrix is lower triangular Given a square matrix and the task is to check the matrix is in lower triangular form or not. A square matrix is called lower triangular if all the entries above the main diagonal are zero. Examples: Input : mat[4][4] = {{1, 0, 0, 0}, {1, 4, 0, 0}, {4, 6, 2, 0}, {0, 4, 7, 6}};Output : Matrix is in l 2 min read Python | Check if two list of tuples are identical Sometimes, while working with tuples, we can have a problem in which we have list of tuples and we need to test if they are exactly identical. This is a very basic problem and can occur in any domain. Let's discuss certain ways in which this task can be done. Method #1 : Using == operator This is th 4 min read Python | Check if element exists in list of lists Given a list of lists, the task is to determine whether the given element exists in any sublist or not. Given below are a few methods to solve the given task. Method #1: Using any() any() method return true whenever a particular element is present in a given iterator. Python3 # Python code to demons 5 min read Python Program to check if matrix is upper triangular Given a square matrix and the task is to check the matrix is in upper triangular form or not. A square matrix is called upper triangular if all the entries below the main diagonal are zero. Examples: Input : mat[4][4] = {{1, 3, 5, 3}, {0, 4, 6, 2}, {0, 0, 2, 5}, {0, 0, 0, 6}}; Output : Matrix is in 2 min read Python - Check if List contains elements in Range Checking if a list contains elements within a specific range is a common problem. In this article, we will various approaches to test if elements of a list fall within a given range in Python. Let's start with a simple method to Test whether a list contains elements in a range.Using any() Function - 3 min read Check if Two Lists Have Same Elements regardless of Order - Python We have multiple ways to check if two lists have the same elements, regardless of their order. The simplest way to check if two lists have the same elements is to sort both lists and then compare them. If both lists have the same elements in the same order after sorting, they are the same.Pythondef 2 min read Python - Check if previous element is smaller in List Sometimes, while working with Python lists, we can have a problem in which we need to check for each element if its preceding element is smaller. This type of problem can have its use in data preprocessing domains. Let's discuss certain problems in which this task can be performed. Input : test_list 5 min read Like