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 8921985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:42:21+00:00 2026-06-15T06:42:21+00:00

Earlier, I was trying to answer a question where I wanted to iterate over

  • 0

Earlier, I was trying to answer a question where I wanted to iterate over a list slice as efficiently as possible.

for x in lst[idx1:]:

isn’t ideal as it creates a copy (In general, this is O(n)). My next thought was to use itertools.islice. But if you look at the documentation, it appears that islice will call next until it finds the index it is looking for at which point it will start to yield values. This is also O(n). It seems that there is an optimization that is available here if the object passed to islice is a list or a tuple — It seems that you could iterate over the “slice” directly (in C) without actually making a copy. I was curious if this optimization is in the source, But I didn’t find anything. I’m not extremely familiar with C and the python source tree, so it’s entirely possible that I missed it.

My question is this:

Is there a way to iterate over a list “slice” without making a copy of the list slice and without burning through a bunch of unwanted elements (in an optimized C implementation)?

I’m well aware that I could write my own generator for this (very naively, not accounting for the fact that many of the arguments should be optional, etc.):

def myslice(obj,start,stop,stride):
    for i in xrange(start,stop,stride):
        yield obj[i]

but that’s definitely not going to beat an optimized C implementation.


If you’re wondering why I would need this over just looping over a slice directly, consider the difference between:

takewhile(lambda x: x == 5, lst[idx:])  #copy's the tail of the list unnecessarily

and

takewhile(lambda x: x == 5, islice(lst,idx,None)) #inspects the head of the list unnecessarily 

and finally:

takewhile(lambda x: x == 5, magic_slice(lst,idx,None)) #How to create magic_slice???
  • 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-06-15T06:42:23+00:00Added an answer on June 15, 2026 at 6:42 am

    Is there a way to iterate over a list “slice” without making a copy of the list slice and without burning through a bunch of unwanted elements (in an optimized C implementation)?

    Yes there is, if you write that C implementation. Cython makes this particularly easy.

    cdef class ListSlice(object):
        cdef object seq
        cdef Py_ssize_t start, end
    
        def __init__(self, seq, Py_ssize_t start, Py_ssize_t end):
            self.seq = seq
            self.start = start
            self.end = end
    
        def __iter__(self):
            return self
    
        def __next__(self):
            if self.start == self.end:
                raise StopIteration()
            r = self.seq[self.start]
            self.start += 1
            return r
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was playing with a fiddle earlier today while trying to answer a question
I asked this question earlier and am now trying to explore the idea of
This is an updated part 2 of a question I asked earlier. I'm trying
This is a related question to one I posted earlier... I'm trying to sum
I'm working from a question I posted earlier ( here ), and trying to
I had a question more detailed earlier which I had no answer, I will
This simple problem is kiling me. I posted something earlier about trying to clean
Based on earlier thread with same argument i am trying to fit a stable
I am trying to learn Ruby on Rails using this Rails Tutorial. Earlier, I
I posted this earlier on wordpress.stackexchange.com. However, never got a reply. Hence, trying my

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.