- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this article, we are going to learn how to make pairs from two lists such that no similar elements make a pair. Follow the below steps to solve the problem.

- Initialize the lists with elements.
- Iterate over the lists and append the pair into a list if the corresponding elements from the lists are not same.
- Print the result.

Let's see the code.

# initializing the lists list_1 = [1, 2, 3, 4, 5] list_2 = [5, 8, 7, 1, 3, 6] # making pairs result = [(i, j) for i in list_1 for j in list_2 if i != j] # printing the result print(result)

If you run the above code, then you will get the following result.

[(1, 5), (1, 8), (1, 7), (1, 3), (1, 6), (2, 5), (2, 8), (2, 7), (2, 1), (2, 3), (2, 6), (3, 5), (3, 8), (3, 7), (3, 1), (3, 6), (4, 5), (4, 8), (4, 7), (4, 1), (4, 3), (4, 6), (5, 8), (5, 7), (5, 1), (5, 3), (5, 6)]

We can solve the problem with itertools module as well. It provides a method called product that makes pairs of all the elements. We can filter the pairs after finding the pairs.

Let's see the code.

# importing the module import itertools # initializing the lists list_1 = [1, 2, 3, 4, 5] list_2 = [5, 8, 7, 1, 3, 6] # pairs pairs = itertools.product(list_1, list_2) # filtering the pairs result = [pair for pair in pairs if pair[0] != pair[1]] # printing the result print(result)

If you run the above code, then you will get the following result.

[(1, 5), (1, 8), (1, 7), (1, 3), (1, 6), (2, 5), (2, 8), (2, 7), (2, 1), (2, 3), (2, 6), (3, 5), (3, 8), (3, 7), (3, 1), (3, 6), (4, 5), (4, 8), (4, 7), (4, 1), (4, 3), (4, 6), (5, 8), (5, 7), (5, 1), (5, 3), (5, 6)]

If you run the above code, then you will get the following result.

- Related Questions & Answers
- Find pairs with given sum such that elements of pair are in different rows in Python
- Find pairs with given sum such that pair elements lie in different BSTs in Python
- Maximum sum from three arrays such that picking elements consecutively from same is not allowed in C++
- Maximum sum such that no two elements are adjacent in C++
- Maximum sum such that no two elements are adjacent - Set 2 in C++
- Ways to paint stairs with two colors such that two adjacent are not yellow in C++
- Python - Check if all elements in a List are same
- Maximum sum in circular array such that no two elements are adjacent in C++
- Check if elements of Linked List are present in pair in Python
- Maximum sum such that no two elements are adjacent Alternate Method in C++ program
- Count pairs of parentheses sequences such that parentheses are balanced in C++
- Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays in Python
- Program to find largest distance pair from two list of numbers in Python
- Count pairs in an array such that both elements has equal set bits in C++
- Maximum sum subarray such that start and end values are same in C++

Advertisements