56. Problem to split a given list into two parts.

In this Python list program, we will take a user input as a list and split a given list into two parts where the length of the first part of the list is given with the help of the below-given steps.

Split the list into two parts:

Steps to solve the program
  1. Take a list as input.
  2. Create an empty list.
  3. Split the given list into two parts where the length of the first part is given using indexing.
  4. Add those parts to the empty list.
  5. Print the list to see the output.
				
					#Input list
list1 = [4, 6, 7, 3, 2, 5, 6, 7, 6, 4]
list2 = []

#length=4
list2.append(list1[:4])
list2.append(list1[4:])

#Printing output
print(list2)
				
			

Output :

				
					[[4, 6, 7, 3], [2, 5, 6, 7, 6, 4]]
				
			

Related Articles

Pack consecutive duplicates of given list

Insert items in the list at a specific location

Leave a Comment