Make pair from two list such that elements are not same in pairs - Python
Last Updated :
30 Jan, 2025
We are given two lists, and our task is to create pairs where the elements in each pair must be different. For example, given a = [1, 2, 3] and b = [2, 3, 4], valid pairs would be [(1, 2), (1, 3), (1, 4), (2, 4), (3, 2), (3, 4)], ensuring that no pair has the same elements. Let's explore different methods to do this in Python.
Using List Comprehension
We can use a simple list comprehension to generate all possible pairs while ensuring that the elements are different.
Python
# Initializing lists
a = [1, 2, 3]
b = [2, 3, 4]
# Generating pairs where elements are different
pairs = [(x, y) for x in a for y in b if x != y]
print(pairs)
Output[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 2), (3, 4)]
Explanation:
- We iterate over each element in a and b.
- The condition x != y ensures that elements in the pair are not the same.
Let's explore some more ways to create pairs where the elements in each pair are different.
itertools.product() function generates all possible pairs, and we can filter out invalid ones using a condition.
Python
from itertools import product
# Initializing lists
a = [1, 2, 3]
b = [2, 3, 4]
# Generating pairs using itertools
pairs = [(x, y) for x, y in product(a, b) if x != y]
print(pairs)
Output[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 2), (3, 4)]
Explanation:
- product(a, b) generates all possible pairs.
- The condition x != y filters out invalid pairs.
- This method is useful for large lists as itertools is optimized for performance.
Using filter() with product()
We can use the filter() function to remove invalid pairs while using product().
Python
from itertools import product
# Initializing lists
a = [1, 2, 3]
b = [2, 3, 4]
# Using filter to remove pairs where elements are the same
pairs = list(filter(lambda pair: pair[0] != pair[1], product(a, b)))
print(pairs)
Output[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 2), (3, 4)]
Explanation:
- product(a, b) generates all possible pairs.
- filter() removes pairs where elements are the same.
Using Nested Loops
We can also use explicit loops to generate pairs while ensuring that elements are different.
Python
# Initializing lists
a = [1, 2, 3]
b = [2, 3, 4]
# Generating pairs using loops
pairs = []
for x in a:
for y in b:
if x != y:
pairs.append((x, y))
print(pairs)
Output[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 2), (3, 4)]
Explanation:
- This method is easy to understand but less concise than list comprehension.
- It is useful when additional conditions need to be applied inside the loop.
Using NumPy for Large Lists
For larger datasets, NumPy provides an efficient way to generate and filter pairs.
Python
import numpy as np
# Initializing lists
a = np.array([1, 2, 3])
b = np.array([2, 3, 4])
# Creating all pairs
pairs = np.array(np.meshgrid(a, b)).T.reshape(-1, 2)
# Filtering out invalid pairs
pairs = pairs[pairs[:, 0] != pairs[:, 1]]
print(pairs)
Output[[1 2]
[1 3]
[1 4]
[2 3]
[2 4]
[3 2]
[3 4]]
Explanation:
- np.meshgrid(a, b) creates a grid of values.
- T.reshape(-1, 2) converts it into a list of pairs.
- The condition pairs[:, 0] != pairs[:, 1] filters out invalid pairs.
Similar Reads
Python program to remove duplicate elements index from other list Given two lists, the task is to write a Python program to remove all the index elements from 2nd list which are duplicate element indices from 1st list. Examples: Input : test_list1 = [3, 5, 6, 5, 3, 7, 8, 6], test_list2 = [1, 7, 6, 3, 7, 9, 10, 11] Output : [1, 7, 6, 9, 10] Explanation : 3, 7 and 1
7 min read
Test if all elements are present in list-Python The task of testing if all elements are present in a list in Python involves checking whether every item in a target list exists within a reference list. For example, given two lists a = [6, 4, 8, 9, 10] and b = [4, 6, 9], the task is to confirm that all elements in list b are also found in list a.U
3 min read
Remove common elements from two list in Python When working with two lists in Python, we may need to remove the common elements between them. A practical example could be clearing out overlapping tasks between two to-do lists. The most efficient way to remove common elements between two lists is by using sets. Pythona = [1, 2, 3, 4, 5] b = [4, 5
3 min read
Python - Filter tuple with all same elements Given List of tuples, filter tuples that have same values. Input : test_list = [(5, 6, 5, 5), (6, 6, 6), (9, 10)] Output : [(6, 6, 6)] Explanation : 1 tuple with same elements. Input : test_list = [(5, 6, 5, 5), (6, 5, 6), (9, 10)] Output : [] Explanation : No tuple with same elements. Method #1 : U
4 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