Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 295097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:25:38+00:00 2026-05-12T06:25:38+00:00

What is the best way to split a list into parts based on an

  • 0

What is the best way to split a list into parts based on an arbitrary number of indexes? E.g. given the code below

indexes = [5, 12, 17]
list = range(20)

return something like this

part1 = list[:5]
part2 = list[5:12]
part3 = list[12:17]
part4 = list[17:]

If there are no indexes it should return the entire list.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:25:38+00:00Added an answer on May 12, 2026 at 6:25 am

    This is the simplest and most pythonic solution I can think of:

    def partition(alist, indices):
        return [alist[i:j] for i, j in zip([0]+indices, indices+[None])]
    

    if the inputs are very large, then the iterators solution should be more convenient:

    from itertools import izip, chain
    def partition(alist, indices):
        pairs = izip(chain([0], indices), chain(indices, [None]))
        return (alist[i:j] for i, j in pairs)
    

    and of course, the very, very lazy guy solution (if you don’t mind to get arrays instead of lists, but anyway you can always revert them to lists):

    import numpy
    partition = numpy.split
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to divide a list into roughly equal parts? For
I'm trying to find the best way to split data into groups, for example
I want to split my list into rows that have all the same number
What is the best way to group an array into a list of array
What's the best way to split a multi-page TIFF with python? PIL doesn't seem
The best way to store images into MySQL is by storing the image location
My current project is split into multiple classes that correctly represent the way we
What do you think is the best way to present a hierarchical list of
What is the best way to pass List via form in python (cgi). ListStr
Possible Duplicate: How do you split a list into evenly sized chunks in Python?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.