- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Python program to find all the Combinations in the list with the given condition
When it is required to find all the combinations in the list with the given condition, a simple iteration, the append method, and the ‘isinstance’ method are used.
Example
Below is a demonstration of the same −
my_list = ["python", [15, 12, 33, 14], "is", ["fun", "easy", "better", "cool"]] print("The list is :") print(my_list) K = 4 print("The value of K is :") print(K) my_result = [] count = 0 while count <= K - 1: temp = [] for index in my_list: if not isinstance(index, list): temp.append(index) else: temp.append(index[count]) count += 1 my_result.append(temp) print("The result is :") print(my_result)
Output
The list is : ['python', [15, 12, 33, 14], 'is', ['fun', 'easy', 'better', 'cool']] The value of K is : 4 The result is : [['python', 15, 'is', 'fun'], ['python', 12, 'is', 'easy'], ['python', 33, 'is', 'better'], ['python', 14, 'is', 'cool']]
Explanation
A list of integers is defined and is displayed on the console.
A value for K is defined and is displayed on the console.
An empty list is created.
A variable ‘count’ is created and is assigned to 0.
A while loop is used to iterate over the list, and the ‘isinstance’ method is used to check if the type of element matches a specific type.
Depending on this, the element is appended to the empty list.
This is the output that is displayed on the console.
- Related Articles
- Python program to find all the Combinations in a list with the given condition
- Program to find list of all possible combinations of letters of a given string s in Python
- Python program to get all pairwise combinations from a list
- C++ Program to Generate All Possible Combinations of a Given List of Numbers
- Program to find length of longest sublist with given condition in Python
- Python Program – Strings with all given List characters
- Python – All combinations of a Dictionary List
- Program to find number of subsequences that satisfy the given sum condition using Python
- Find all triplets in a list with given sum in Python
- Python - Find all the strings that are substrings to the given list of strings
- Python program to replace all Characters of a List except the given character
- Python program to multiply all numbers in the list?
- Find the lexicographically smallest string which satisfies the given condition in Python
- Python Program to Accept Three Digits and Print all Possible Combinations from the Digits
- Program to find the sum of all digits of given number in Python

Advertisements