
- 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
Python program to get all pairwise combinations from a list
When it is required to get all pairwise combinations from a list, an iteration along with the ‘append’ method is used.
Example
Below is a demonstration of the same
my_list = [15,"John", 2, "Will", 53, 'Rob'] print("The list is :") print(my_list) my_result = [] for i in range(0,len(my_list)): for j in range(0,len(my_list)): if (i!=j): my_result.append((my_list[i],my_list[j])) print("The result is :") print(my_result)
Output
The list is : [15, 'John', 2, 'Will', 53, 'Rob'] The result is : [(15, 'John'), (15, 2), (15, 'Will'), (15, 53), (15, 'Rob'), ('John', 15), ('John', 2), ('John', 'Will'), ('John', 53), ('John', 'Rob'), (2, 15), (2, 'John'), (2, 'Will'), (2, 53), (2, 'Rob'), ('Will', 15), ('Will', 'John'), ('Will', 2), ('Will', 53), ('Will', 'Rob'), (53, 15), (53, 'John'), (53, 2), (53, 'Will'), (53, 'Rob'), ('Rob', 15), ('Rob', 'John'), ('Rob', 2), ('Rob', 'Will'), ('Rob', 53)]
Explanation
A list is defined and is displayed on the console.
An empty list is defined.
The original list is iterated over, and again iterated over using two iterations in all.
When both the indices are not equal, the respective elements of the list are appended to the empty list.
This is the result which is displayed as output on the console.
- Related Questions & Answers
- Python – All combinations of a Dictionary List
- Python Program to get all unique keys from a List of Dictionaries
- Python program to find all the Combinations in a list with the given condition
- Python program to find all the Combinations in the list with the given condition
- C++ Program to Generate All Possible Combinations of a Given List of Numbers
- How to get a list of all the keys from a Python dictionary?
- How to get a list of all the values from a Python dictionary?
- Python Program to Accept Three Digits and Print all Possible Combinations from the Digits
- Program to find list of all possible combinations of letters of a given string s in Python
- How to get all combinations of some arrays in JavaScript?
- Program to swap string characters pairwise in Python
- Python program to find all close matches of input string from a list
- Python program to print all sublists of a list.
- Java Program to Get Minimum and Maximum From a List
- Python program to extract Keywords from a list
Advertisements