Open In App

Python Program to Split a List into Two Halves

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, splitting a list into two halves is a common task that can be accomplished using various methods. In this article, we will explore four commonly used methods to split a list into two halves and provide a detailed explanation for each. Additionally, we will print the output for better understanding.

Python Program To Split A List Into Two Halves

Below, are the methods of Python Program To Split A List Into Two Halves.

Python Program To Split A List Into Two Halves Using Slicing

In this method, we calculate the middle index of the list using the length of the list divided by 2. Then, we use slicing to create two halves – one from the beginning to the middle index, and the other from the middle index to the end.

Python3




def split_list_slicing(input_list):
    length = len(input_list)
    middle = length // 2
    first_half = input_list[:middle]
    second_half = input_list[middle:]
    return first_half, second_half
 
# Example Usage
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
output = split_list_slicing(my_list)
print("Slicing:")
print("First Half:", output[0])
print("Second Half:", output[1])


Output

Slicing:
First Half: [1, 2, 3, 4]
Second Half: [5, 6, 7, 8]

Python Program To Split A List Into Two Halves Using List Comprehension

Here, we use list comprehension to create two halves of the list. The first_half comprehends elements from the start to the middle index, and the second_half comprehends elements from the middle index to the end.

Python3




def split_list(input_list):
    middle = len(input_list) // 2
    first_half = [input_list[i] for i in range(middle)]
    second_half = [input_list[i] for i in range(middle, len(input_list))]
    return first_half, second_half
 
# Example Usage
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
output = split_list(my_list)
print("\List Comprehension:")
print("First Half:", output[0])
print("Second Half:", output[1])


Output

\List Comprehension:
First Half: [1, 2, 3, 4]
Second Half: [5, 6, 7, 8]

Python Program To Split A List Into Two Halves Using Iterative Approach

In this method, we use an iterative approach to build the first and second halves of the list using a for loop. Elements are appended to the halves based on their position in the original list.

Python3




def split_list(input_list):
    middle = len(input_list) // 2
    first_half = []
    second_half = []
    for i in range(middle):
        first_half.append(input_list[i])
    for i in range(middle, len(input_list)):
        second_half.append(input_list[i])
    return first_half, second_half
 
# Example Usage
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
output = split_list(my_list)
print("\Iterative:")
print("First Half:", output[0])
print("Second Half:", output[1])


Output

\Iterative:
First Half: [1, 2, 3, 4]
Second Half: [5, 6, 7, 8]

Python Program To Split A List Into Two Halves Using NumPy Library

NumPy is a powerful library for numerical operations in Python. In this method, we convert the input list into a NumPy array and then use array slicing to split it into two halves.

Python3




import numpy as np
 
def split_list(input_list):
    array_list = np.array(input_list)
    middle = len(array_list) // 2
    first_half = array_list[:middle].tolist()
    second_half = array_list[middle:].tolist()
    return first_half, second_half
 
# Example Usage
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
output = split_list(my_list)
print("\nNumPy:")
print("First Half:", output[0])
print("Second Half:", output[1])


Output

NumPy:
First Half: [1, 2, 3, 4]
Second Half: [5, 6, 7, 8]

Conclusion

In this Python program, we explored different methods to split a list into two halves. Depending on our specific requirements, we can choose the method that best fits us needs. Each method has its own advantages, and understanding these techniques will enhance your ability to manipulate lists in Python



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads