- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
All Combinations For A List Of Objects
Printing all the combinations for a list of objects is one of the common operations that we can perform on a given list. The 'itertools' module of Python provides some built-in methods that are efficient and easy to use, making it simple to generate possible combinations of list objects. We are going to learn how we can use the 'itertools' module through this article.
Python program to Print all Combinations for a list of Objects
Let's discuss the in-built methods of itertools along with their example program that will show us how to produce the combinations for a list of objects.
itertools
It is a fast and memory-efficient tool that is used to work with iterable objects. To use this module in our program we need to import it using the following command:
import itertools
combinations()
This method is best suited when we work with permutation and combination. It takes two parameters and generates all possible combinations of a given length from a list of objects.
Syntax
combinations(nameOfiterator, length)
Here
nameOfiterator specifies the iterable object for which we need combinations.
length specifies the length of combination.
Example 1
The following example illustrates how 'combinations()' method is used to generate combinations for list objects.
Approach
The first step is to import the 'itertools' module.
Create a user-defined method named 'get_combinations()' along with a parameter.
Initialize an empty list called 'combination' to store all the combinations.
Take a for loop to iterate over the input list ranging from 1 to the length of list.
Now, call the 'itertools.combinations()' method to generate all possible combinations of length r from the specified input list. Also, we need to extend the combination list by appending the generated combinations.
Create another list named 'objects' containing three elements.
In the end, call the 'get_combinations()' method with 'objects' as an argument to generate combinations.
import itertools def get_combinations(lst): # creating a user-defined method combination = [] # empty list for r in range(1, len(lst) + 1): # to generate combination combination.extend(itertools.combinations(lst, r)) return combination objects = ['9', '8', '0'] # creating a list named objects all_combinations = get_combinations(objects) # method call print(all_combinations)
Output
[('9',), ('8',), ('0',), ('9', '8'), ('9', '0'), ('8', '0'), ('9', '8', '0')]
Example 2
In this example, we will use '+=' operator rather than the 'extend' keyword to append all the combinations in one single list.
import itertools def get_combinations(lst): # creating a user-defined method combination = [] # empty list for r in range(1, len(lst) + 1): # to generate combination combination += itertools.combinations(lst, r) return combination objects = ['9', '8', '0'] # creating a list named objects all_combinations = get_combinations(objects) # method call print(all_combinations)
Output
[('9',), ('8',), ('0',), ('9', '8'), ('9', '0'), ('8', '0'), ('9', '8', '0')]
product()
This method is used to return the Cartesian product of the specified iterator. It takes an iterable object and an integer as parameters. The integer here specifies the repetition of objects.
Syntax
combinations(nameOfiterator, repeat = r)
Example 3
In this example, we will use the code from the previous example with a few changes. We will use the built-in method 'product()' instead of 'combinations()'. Rest of the code will work same as previous one but, it will allow repetition of objects.
import itertools def get_combinations(lst): # creating a user-defined function combination = [] # empty list for r in range(1, len(lst) + 1): # to generate combination combination.extend(itertools.product(lst, repeat=r)) return combination objects = ['9', '8', '0'] # creating a list named objects all_combinations = get_combinations(objects) print(all_combinations)
Output
[('9',), ('8',), ('0',), ('9', '9'), ('9', '8'), ('9', '0'), ('8', '9'), ('8', '8'), ('8', '0'), ('0', '9'), ('0', '8'), ('0', '0'), ('9', '9', '9'), ('9', '9', '8'), ('9', '9', '0'), ('9', '8', '9'), ('9', '8', '8'), ('9', '8', '0'), ('9', '0', '9'), ('9', '0', '8'), ('9', '0', '0'), ('8', '9', '9'), ('8', '9', '8'), ('8', '9', '0'), ('8', '8', '9'), ('8', '8', '8'), ('8', '8', '0'), ('8', '0', '9'), ('8', '0', '8'), ('8', '0', '0'), ('0', '9', '9'), ('0', '9', '8'), ('0', '9', '0'), ('0', '8', '9'), ('0', '8', '8'), ('0', '8', '0'), ('0', '0', '9'), ('0', '0', '8'), ('0', '0', '0')]
Conclusion
We started this article by addressing the given problem and introducing a possible solution. Then, in the later sections, we learned the 'itertools' module and its built-in methods. With the help of example programs, we discussed the use of these methods in generating all combinations for a list of objects.