A = [1,2,3,4,5,6]
B = A[:len(A)//2]
C = A[len(A)//2:]
If you want a function:
def split_list(a_list):
half = len(a_list)//2
return a_list[:half], a_list[half:]
A = [1,2,3,4,5,6]
B, C = split_list(A)
Answer from Jason Coon on Stack Overflow Top answer 1 of 16
361
A = [1,2,3,4,5,6]
B = A[:len(A)//2]
C = A[len(A)//2:]
If you want a function:
def split_list(a_list):
half = len(a_list)//2
return a_list[:half], a_list[half:]
A = [1,2,3,4,5,6]
B, C = split_list(A)
2 of 16
109
A little more generic solution (you can specify the number of parts you want, not just split 'in half'):
def split_list(alist, wanted_parts=1):
length = len(alist)
return [ alist[i*length // wanted_parts: (i+1)*length // wanted_parts]
for i in range(wanted_parts) ]
A = [0,1,2,3,4,5,6,7,8,9]
print split_list(A, wanted_parts=1)
print split_list(A, wanted_parts=2)
print split_list(A, wanted_parts=8)
Reddit
reddit.com โบ r/learnpython โบ splitting an array into two and swapping the first half with the second half
r/learnpython on Reddit: Splitting an Array into two and Swapping the First Half With the Second Half
May 18, 2020 -
Here's the algorithm:
array = [int(num) for num in input().split()]
len = len(array)
mid=int(len/2)
if mid%2==0:
for i in range(mid):
temp=array[i]
array[i]=array[mid+i]
array[mid + i]=temp
else:
for i in range(mid):
temp=array[i]
array[i]=array[mid+i+1]
array[mid + i + 1]=temp
print(array)The issue is, the program has to decide between two loops depending on the length of the array (odd or even), which I think is very inefficient. Can someone suggest a way to bring this down to a single loop?
Sample I/O for even:
1 2 3 4 5 6 7 8 [5, 6, 7, 8, 1, 2, 3, 4]
Sample I/O for odd:
1 2 3 4 5 6 7 [5, 6, 7, 4, 1, 2, 3]
Top answer 1 of 5
4
So this problem could actually be solved without any loops at all, just by using array splicing, you may want to take a look at that. Also, I'm not 100% sure, but I think in your odd example the output isn't actually what you want, since the 4 is out of place
2 of 5
3
array[len(array)//2:] + array[:len(array)//2] should do it without a need of any loops or branches.
python - separate an array into two halves - Stack Overflow
I like to split an array into first half and its second half. More on stackoverflow.com
How can I split a Python list in half? - Ask a Question - TestMu AI (formerly LambdaTest) Community
How can I split a Python list in half? For example, if I have the list: A = [0, 1, 2, 3, 4, 5] I want to split it into two smaller lists: B = [0, 1, 2] C = [3, 4, 5] What is the best way to achieve this in Python? More on community.testmuai.com
Split array Python - Stack Overflow
Not splitting the array into half. Btw I got the answer. Thanks for the comment. ... Your welcome, the article was specifically about splitting in half but you can use the implementation for your purposes :) ... Sign up to request clarification or add additional context in comments. ... should do it, assuming list1 is a standard python ... More on stackoverflow.com
python - What is the most efficient way to split a list evenly in half - Stack Overflow
I need to split a list into halves such that if it has an odd length the middle element is ignored entirely, I have a function for this, but it's quite slow for what I'm trying to do. My function (variables renamed because they make no sense out of context): Copydef splitList(array): half = ... More on stackoverflow.com
pythoncodelab
pythoncodelab.com โบ home โบ how to split a list in half python
How to split a list in half Python - - pythoncodelab
November 2, 2024 - We have NumPy inbuilt function which helps to split the list into half. You have to first install NumPy by running the following command โpip install numpyโ in the command prompt. Now letโs explore the code. import numpy as np def split_list_into_approx_equal_parts(lst, parts=2): return np.array_split(lst, parts) my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] parts_np = split_list_into_approx_equal_parts(my_list, parts=2) print("Parts (numpy array_split):", parts_np)
TutorialsPoint
tutorialspoint.com โบ python-program-to-split-a-list-into-two-halves
Python Program to Split a List into Two Halves
April 21, 2023 - The most straightforward method to split a list is using Python's slicing technique. This approach divides the list at a specific index, creating two separate parts. When a list has an even number of elements, splitting results in two equal halves ?
Top answer 1 of 3
4
You are doing float-math - use integer division:
A = [1,2,3,4,5,6]
B = A[:len(A)//2]
C = A[len(A)//2:]
print(A,B,C)
Output:
([1, 2, 3, 4, 5, 6], [1, 2, 3], [4, 5, 6])
Have a look at the operators here: numeric-types-int-float-complex
2 of 3
3
try this
A = [1,2,3,4,5,6]
half = len(A)//2
B = A[:half]
C = A[half:]
NumPy
numpy.org โบ doc โบ stable โบ reference โบ generated โบ numpy.split.html
numpy.split โ NumPy v2.5 Manual
If indices_or_sections is an integer, N, the array will be divided into N equal arrays along axis. If such a split is not possible, an error is raised.
datagy
datagy.io โบ home โบ python posts โบ python lists โบ python: split a list (in half, in chunks)
Python: Split a List (In Half, in Chunks) โข datagy
December 30, 2022 - This function allows you to split an array into a set number of arrays. Letโs see how we can use NumPy to split our list into 3 separate chunks: # Split a Python List into Chunks using numpy import numpy as np a_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] our_array = np.array(a_list) chunked_arrays = np.array_split(our_array, 3) chunked_list = [list(array) for array in chunked_arrays] print(chunked_list) # Returns: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
GeeksforGeeks
geeksforgeeks.org โบ python โบ splitting-arrays-in-numpy
Splitting Arrays in NumPy - GeeksforGeeks
December 23, 2025 - Explanation: Array shape is (2, 3, 4) and dsplit(..., 2) splits last axis into 2 equal parts. Each result contains half of the last dimension
NumPy
numpy.org โบ devdocs โบ reference โบ generated โบ numpy.split.html
numpy.split โ NumPy v2.6.dev0 Manual
If indices_or_sections is an integer, N, the array will be divided into N equal arrays along axis. If such a split is not possible, an error is raised.
GeeksforGeeks
geeksforgeeks.org โบ python-program-for-split-the-array-and-add-the-first-part-to-the-end
Python Program to Split the array and add the first part to the end | GeeksforGeeks
March 28, 2023 - Use a list comprehension to create a new list result of the same length as the input array, where each element of the new list is computed using the formula (i + n) % arr_len where i is the index of the current element in the input array. Return the new list result. ... def split_and_add(arr, n): return [arr[(i + n) % len(arr)] for i in range(len(arr))] arr = [12, 10, 5, 6, 52, 36] n = 2 result = split_and_add(arr, n) print(*result)
Kanoki
kanoki.org โบ 2020 โบ 06 โบ 11 โบ how-to-split-numpy-arrays
How to split Numpy Arrays | kanoki
June 11, 2020 - In this post we will see how to split a 2D numpy array using split, array_split , hsplit, vsplit and dsplit.
Real Python
realpython.com โบ how-to-split-a-python-list-into-chunks
How to Split a Python List or Iterable Into Chunks โ Real Python
January 27, 2025 - If your data is strictly numeric, then you can try leveraging one of the functions provided by the esteemed NumPy library to split a sequence of numbers into chunks. Thanks to its unmatched speed, NumPy is well suited to all kinds of number crunching, which would otherwise run significantly slower when implemented in pure Python. ... This scientific library comes with several array-splitting functions, which you can use to divide an array into smaller parts.