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


The program creates a list named val which contains strings and nested lists. It then defines a variable a which is equal to 2. The program then creates an empty list called l and a variable c which is initialized to 0. The program then enters a while loop that continues to execute as long as c is less than or equal to a - 1. Inside the while loop, the program creates an empty list called t.

The program then uses a for loop to iterate through each element of the val list. Within the for loop, the program checks if the current element is not an instance of a list. If it is not, the program appends the current element to the t list. If it is an instance of a list, the program appends the element at index c of the current element to the t list.

The program then increments c by 1 and appends the t list to the l list. After the while loop, the program prints the l list which contains the combinations of the indexes of the elements in val list.

Source Code

val = ["Tutor Joes",["Software","Computer"],
			["Solution", "Education"]]
print("Original List : " + str(val))
a = 2
l = []
c = 0
while c <= a - 1:
	t = []
	for i in val:
		if not isinstance(i, list):
			t.append(i)
		else:
			t.append(i[c])
	c += 1
	l.append(t)
 
print("\nIndex Combinations : " + str(l))

Output

Original List : ['Tutor Joes', ['Software', 'Computer'], ['Solution', 'Education']]

Index Combinations : [['Tutor Joes', 'Software', 'Solution'], ['Tutor Joes', 'Computer', 'Education']]


Example Programs