- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Split a List into Two Halves
In Python, a single variable can contain multiple items by using lists. One of the four builtin data types for storing data collections in Python is a list; the other three are tuples, sets and dictionaries, each with its own purpose.
What Is List?
Square brackets are used to build lists.The most effective tool in Python is the list because they don't necessarily have to be homogeneous. DataTypes like Integers, Strings, and Objects can all be found in one list. Because lists are mutable, changes can be made to them even after they have been created.
In this article, we will explore various methods for dividing a list into halves using Python programming. Lists are one of the mutable data types that can store a collection of objects. With these techniques, you will be able to divide any list in half with ease!
Using The Slicing Technique
In the first scenario, the list is divided in half or two halves. Depending on the length of the list, these halves can be either equal or uneven in size. The list can be split up using the slicing method.
Create a list and initialize its mid index using half of its length.
Split it in two halves from the start to mid-index and from mid-index till end respectively.
Print out both original list, as well as each split half.
Sort each of these halves before merging them together into a single, sorted list.
Finally, print out this new merged, sorted list.
The following example creates a list with 6 elements, then sets the index to 3. It then splits the list into two halves based on that index - the first half is all of the elements before the index, and second half is all of the elements after it. Finally, it prints out both halves of the list.
#create list list_1 = [10,20,30,40,50,60] index = 3 first_half = list_1 [:index] second_half = list_1 [index:] print('The primary list is: ',list_1) print("First half of list is ",first_half) print("Second half of list is ",second_half)
The primary list is: [10, 20, 30, 40, 50, 60] First half of list is [10, 20, 30] Second half of list is [40, 50, 60]
Here, in the method explained above we had the index and the length of the list was predefined. What if the dividing index or the size of the two parts are not specified? The next step is to determine the list's middle index, which may be done by multiplying the list's length by 2. However, if the list's length is an odd integer or the list is not symmetrical, we will obtain a float value when we divide the list. To round the result, we will use the floor operator (//).
In this method, our main focus is on solving a different condition that is, if the elements asked from the user is odd in number, then what will be the process of completing the task. Here, the split function returns two unequal lists since the list has an odd number of elements. The midway is (5/2) = 2.5 because the list is 5 items long. The closest integer value that is less than or equal to a division result is returned by the floor operator. The floor operator in this instance yields 2 rather than 2.5.
Define a function that takes in a list of numbers and asks the user for an input value.
Use a for loop to iterate through the list,
Then use the append() function to divide each number by 2 and find its middle index.
Prompt the user for their input when complete.
The following example shows that the program takes a list of numbers as input from the user and splits it into two halves. It asks the user to enter the number of elements they want in their list and then prompts them to enter each element one at a time.
The middle index is calculated by dividing the length of the list by 2, then using this index it calls split_list() which uses slicing to separate the first half and second half of the list and returns both lists separately.
def split_list(input_L,n): first_half = input_L[:n] second_half = input_L[n:] return first_half,second_half if __name__ == "__main__" : list_1 =  length = int(input("Enter the number of elements you want in list : ")) for i in range(0, length): item = int(input("Enter the element for list "+str(i+1)+" :")) list_1.append (item) middle_index = length//2 first,second = split_list (list_1,middle_index) print ("Primary list: ", list_1) print ("First half of the list is: ", first) print ("second half of the list is: ", second)
On executing the above program, the following output is generated −
Enter the number of elements you want in list: 5 Enter the element for list 1:98 Enter the element for list 2:60 Enter the element for list 3:45 Enter the element for list 4:33 Enter the element for list 5:55 Primary list: [98, 60, 45, 33, 55] First half of the list is: [98, 60] second half of the list is: [45, 33, 55]
In this article, we have used different ways of splitting the list into two halves using python.
Kickstart Your Career
Get certified by completing the courseGet Started