48. Problem to create a list of sublists.

In this Python list program, we will take two lists as input and iterate over lists to create a list of sublists with the help of the below-given steps.

Create a list of sublists:

Steps to solve the program
  1. Take two lists as inputs.
  2. Create an empty list to store a list of sublists.
  3. Use for loop and zip function to combine the input lists and add them to the empty list.
  4. Print the list to see the output.
				
					#Input lists
list1 = [1, 3, 5, 7, 9]
list2 = [8, 6, 4, 2, 10]

#Creating empty list
empty_list = []
for (a,b) in zip(list1,list2):
    elements = [a, b]
    empty_list.append(elements)

#printing output    
empty_list
				
			

Output :

				
					[[1, 8], [3, 6], [5, 4], [7, 2], [9, 10]]
				
			

Related Articles

Insert a given string at the beginning

Move all positive numbers on the left

Leave a Comment