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)
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
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): def splitList(array): half = ... More on stackoverflow.com
Splitting an Array into two and Swapping the First Half With the Second Half
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 More on reddit.com
Text file list split in half and save into two files
Don't name your variable 'file', that is a reserved word in Python. And, the same with 'list': with open('file.txt') as fh_read, open('file1.txt', 'w') as fh_write1, open('file2.txt', 'w') as fh_write2: entries = fh_read.readlines() [fh_write1.write(entry) for entry in entries[:len(entries) // 2]] [fh_write2.write(entry) for entry in entries[len(entries) // 2:]] Edit: Fair argument from u/lanemik ; upvote tossed over there. Though, I'd not strip() because that could remove important characters. I'm sticking with readlines() and do not mind comprehensions without assignment, personally. More on reddit.com
02:11
How To Split Any List Into Equally-Sized Chunks (Python Recipes) ...
02:14
Split list into smaller lists (split in half) - YouTube
04:01
Python : How do you split a list into evenly sized chunks? - YouTube
13:15
How to Divide Each Element in a List in Python - YouTube
01:11
PYTHON : Split list into smaller lists (split in half) - YouTube
04:36
How to split a list into evenly sized chunks in Python | Python ...
TutorialsPoint
tutorialspoint.com › article › python-program-to-split-a-list-into-two-halves
Python Program to Split a List into Two Halves
March 27, 2026 - 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 ?
AdamSmith.haus
adamsmith.haus › python › answers › how-to-split-a-list-in-half-in-python
How to split a list in half in Python - Adam Smith
Python answers, examples, and documentation
Finxter
blog.finxter.com › home › learn python blog › how to split a list in half in 5 ways
How to Split a List in Half in 5 Ways - Be on the Right Side of Change
July 15, 2022 - Above defines a function with one (1) argument (split_half(pop)). This function splits the list in half using the Right Shift Operator and returns the results as a Tuple with two (2) nested lists.
Finxter
blog.finxter.com › 5-best-ways-to-split-a-python-list-into-two-halves
5 Best Ways to Split a Python List into Two Halves – Be on the Right Side of Change
A one-liner approach can be achieved by harnessing Python’s slice notation in a concise expression that is suitable for quickly splitting a list within a single line of code. ... original_list = [42, 55, 63, 17, 22] first_half, second_half = original_list[:len(original_list)//2], original_list[len(original_list)//2:] print(first_half) print(second_half)
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.
GeeksforGeeks
geeksforgeeks.org › python-ways-to-spilt-the-list-by-some-value
How to Split the list by some value ? - GeeksforGeeks
December 18, 2024 - Method #1 : Using itemgetter ... Splitting a list into two halves is a common operation that can be useful in many cases, such as when dealing with large datasets or when performing specific algorithms (e.g., merge sort).
Reddit
reddit.com › r/learnpython › text file list split in half and save into two files
r/learnpython on Reddit: Text file list split in half and save into two files
October 22, 2018 -
I have a list of items importing list so:
file = open('file.txt', 'r')
list = file.readlines()
total = len(list) # 1372
half = total/2 # 686
print (half) Kinda proud I got this far... However the next part stumps me, how can I take my list and divide it by half and save into two files? Say... file1.txt, file2.txt
Thx!
GeeksforGeeks
geeksforgeeks.org › python › how-to-split-lists-in-python
How to Split Lists in Python? - GeeksforGeeks
July 23, 2025 - Explanation: Slicing operator : is used to divide the list into two parts, with the first slice containing elements up to index 3 and the second slice starting from index 3. For situations where you need to split a list into chunks of equal ...