• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / How to Split List in Half Using Python

How to Split List in Half Using Python

June 25, 2022 Leave a Comment

To split a list in half using Python, the easiest way is with list slicing.

list_of_numbers = [0, 1, 2, 3, 4, 5]

first_half = list_of_numbers[:3]
second_half = list_of_numbers[3:]

print(first_half)
print(second_half)

#Output:
[0, 1, 2]
[3, 4, 5]

You can easily create a function which will find the middle position of a given list and split a list in half in Python.

def split_list_in_half(lst):
    middle = len(lst) // 2
    return [lst[:middle],lst[middle:]]

list_of_numbers = [0, 1, 2, 3, 4, 5, 6, 7]

print(split_list_in_half(list_of_numbers))

#Output:
[[0, 1, 2, 3], [4, 5, 6, 7]]

When working with lists in Python, the ability to manipulate them and create new lists is very valuable.

One such manipulation is the ability to split a list into half using Python.

To split a list in half, we just need to know the length of the list and then we can divide by two to get the middle position of the list.

Then to split a list in half, we can use list slicing.

Below is a simple function which will split a list in half for you in Python. Note: we use integer division so that we can use an integer for our slice.

def split_list_in_half(lst):
    middle = len(lst) // 2
    return [lst[:middle],lst[middle:]]

list_of_numbers = [0, 1, 2, 3, 4, 5, 6, 7]

print(split_list_in_half(list_of_numbers))

#Output:
[[0, 1, 2, 3], [4, 5, 6, 7]]

How to Split a List into Equal Sublists Using Python

If you want to split a list into more than two equal pieces, for example, to split a list into thirds, we can generalize our solution from above.

In Python, we can split a list into n sublists a number of different ways.

Given a length, or lengths, of the sublists, we can use a loop, or list comprehension to split a list into n sublists.

To split a list into n sublists, first we need to calculate the length of each sublist. While it might not always work out that there are equal lengths for all n sublists, we can get pretty close.

After getting the length of each sublist, we can then loop over the list and create a list of lists with slicing.

Below is a Python function which will split a list into n sublists with a for loop.

list_of_numbers = [0,1,2,3,4,5]

def getSublists(lst,n):
    subListLength = len(lst) // n
    list_of_sublists = []
    for i in range(0, len(lst), subListLength):
        list_of_sublists.append(lst[i:i+subListLength])
    return list_of_sublists

print(getSublists(list_of_numbers, 3))

#Output:
[[0, 1], [2, 3], [4, 5]]

Hopefully this article has been useful for you to learn how to split a list in half using Python.

Other Articles You'll Also Like:

  • 1.  Python tostring method – Convert an Object to String with str() Function
  • 2.  pandas idxmax – Find Index of Maximum Value of Series or DataFrame
  • 3.  How to Return Nothing in Python from Function
  • 4.  Python atanh – Find Hyperbolic Arctangent of Number Using math.atanh()
  • 5.  pandas percentile – Calculate Percentiles of Series or Columns in DataFrame
  • 6.  Python Round to Nearest 10 With round() Function
  • 7.  Get Substring Between Two Characters with Python
  • 8.  Python Split List into N Sublists
  • 9.  Using Python to Check If a Number is a Perfect Square
  • 10.  Find Index of Minimum in List Using Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy