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
- Take a list as input.
- Create an empty list.
- Split the given list into two parts where the length of the first part is given using indexing.
- Add those parts to the empty list.
- 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
Python program to insert items at a specific position in the list.
Python program to select random numbers from the list.
Python program to create a 3*3 grid with numbers.
Python program to zip two lists of lists into a list.
Python program to convert the first and last letter of each item from Upper case and lowercase.
Python to find maximum and minimum values in the given heterogeneous list.