Python program to find all the Combinations in a list with the given condition

When it is required to find all the combinations in a list with a specific condition, a simple iteration, the ‘isinstance’ method, the ‘append’ method and indexing are used.

Example

Below is a demonstration of the same −

print("Method definition begins")
def merge_the_vals(my_list_1, my_list_2, K):
   index_1 = 0
   index_2 = 0
   while(index_1 

Output

Method definition begins
Method definition ends
The first list is :
[12, 56, 14, 28, 61, 73, 59, 90]
The second list is :
[52, 16, 17, 34, 43, 16, 84, 57]
The value of K is
2
The resultant list is :
[12, 56, 52, 16, 14, 28, 17, 34, 61, 73, 43, 16, 59, 90, 84, 57]
The list after sorting is :
[12, 14, 16, 16, 17, 28, 34, 43, 52, 56, 57, 59, 61, 73, 84, 90]

Explanation

  • A method is defined that takes two lists and a value of K as a parameter.

  • Depending on the value of index and length of list, the ‘yield’ operator is used to give result.

  • Outside the method, two lists of integers are defined and are displayed on the console.

  • The value for K is defined and displayed in the console.

  • A list comprehension is used, the method is called by passing the required parameters.

  • This is assigned to a result.

  • This is displayed as output on the console.

  • The result is sorted using a sort method and is displayed on the console.

Updated on: 2021-09-13T10:40:48+05:30

668 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements