Python List Comprehension to find pair with given sum from two arrays Last Updated : 28 Jul, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given two unsorted arrays of distinct elements, the task is to find all pairs from both arrays whose sum is equal to x. Examples: Input : arr1 = [-1, -2, 4, -6, 5, 7] arr2 = [6, 3, 4, 0] x = 8 Output : [(5, 3), (4, 4)] Input : arr1 = [1, 2, 4, 5, 7] arr2 = [5, 6, 3, 4, 8] x = 9 Output : [(1, 8), (4, 5), (5, 4)] We have existing solution for this problem please refer Given two unsorted arrays, find all pairs whose sum is x link. We can solve this problem quickly in python using List comprehension. Approach is very simple, we will consider all those pairs for which if k lies in arr2 then x-k should lie in arr1, so pair will be (x-k,k). Implementation: Python3 # Function to find all pairs whose sum is x in # two arrays def allPairs(arr1,arr2,x): # finds all pairs in two arrays # whose sum is x print ([(x-k,k) for k in arr2 if (x-k) in arr1]) # Driver program if __name__ == "__main__": arr1 = [-1, -2, 4, -6, 5, 7] arr2 = [6, 3, 4, 0] x = 8 allPairs(arr1,arr2,x) Output[(5, 3), (4, 4)] Complexity : O(n*n) Comment More infoAdvertise with us Next Article Python - Find missing and additional values in two lists S Shashank Mishra Follow Improve Article Tags : Python python-list Python list-programs Practice Tags : pythonpython-list Similar Reads Given two arrays, find n+m-1 unique sum pairs Given 2 integer arrays A and B(with no repeated elements in each) of size N and M respectively, form N + M - 1 pairs, each pair having one element from both the arrays, such that the sum of all the pairs is unique. For each pair, print the index of the elements in their respective array. Example: In 3 min read Python Program to Find Sum of Array Given an array of integers, find the sum of its elements. Examples: Input : arr[] = {1, 2, 3}Output : 6Explanation: 1 + 2 + 3 = 6This Python program calculates the sum of an array by iterating through each element and adding it to a running total. The sum is then returned. An example usage is provid 4 min read Python | Extract Combination Mapping in two lists Sometimes, while working with Python lists, we can have a problem in which we have two lists and require to find the all possible mappings possible in all combinations. This can have possible application in mathematical problems. Let's discuss certain way in which this problem can be solved. Method 2 min read Subtract two list elements if element in first list is greater - Python We are given two lists of equal length. Our task is to subtract the corresponding elements of these lists, but only if the element from the first list is greater than the corresponding element from the second list. If the element from the first list is smaller or equal, we leave the result as zero o 3 min read Python - Find missing and additional values in two lists It's common to compare two lists and determine which values are missing or additional in each. For example, we might have a list of expected items and another list showing what we have in stock. We can easily identify missing and additional items by comparing the two lists. List comprehension is a c 3 min read Python | Count of common elements in the lists Sometimes, while working with Python list we can have a problem in which we have to compare two lists for index similarity and hence can have a task of counting equal index pairs. Let's discuss certain ways in which this task can be performed. Method #1: Using sum() + zip() This task can be performe 5 min read Like