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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:46:31+00:00 2026-05-20T08:46:31+00:00

I have the following problem. Given a list of integers L , I need

  • 0

I have the following problem.

Given a list of integers L, I need to generate all of the sublists L[k:] for k in [0, len(L) - 1], without generating copies.

How do I accomplish this in Python? With a buffer object somehow?

  • 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-20T08:46:31+00:00Added an answer on May 20, 2026 at 8:46 am

    The short answer

    Slicing lists does not generate copies of the objects in the list; it just copies the references to them. That is the answer to the question as asked.

    The long answer

    Testing on mutable and immutable values

    First, let’s test the basic claim. We can show that even in the case of immutable objects like integers, only the reference is copied. Here are three different integer objects, each with the same value:

    >>> a = [1000 + 1, 1000 + 1, 1000 + 1]
    

    They have the same value, but you can see they are three distinct objects because they have different ids:

    >>> map(id, a)
    [140502922988976, 140502922988952, 140502922988928]
    

    When you slice them, the references remain the same. No new objects have been created:

    >>> b = a[1:3]
    >>> map(id, b)
    [140502922988952, 140502922988928]
    

    Using different objects with the same value shows that the copy process doesn’t bother with interning — it just directly copies the references.

    Testing with mutable values gives the same result:

    >>> a = [{0: 'zero', 1: 'one'}, ['foo', 'bar']]
    >>> map(id, a)
    [4380777000, 4380712040]
    >>> map(id, a[1:]
    ... )
    [4380712040]
    

    Examining remaining memory overhead

    Of course the references themselves are copied. Each one costs 8 bytes on a 64-bit machine. And each list has its own memory overhead of 72 bytes:

    >>> for i in range(len(a)):
    ...     x = a[:i]
    ...     print('len: {}'.format(len(x)))
    ...     print('size: {}'.format(sys.getsizeof(x)))
    ... 
    len: 0
    size: 72
    len: 1
    size: 80
    len: 2
    size: 88
    

    As Joe Pinsonault reminds us, that overhead adds up. And integer objects themselves are not very large — they are three times larger than references. So this saves you some memory in an absolute sense, but asymptotically, it might be nice to be able to have multiple lists that are “views” into the same memory.

    Saving memory by using views

    Unfortunately, Python provides no easy way to produce objects that are “views” into lists. Or perhaps I should say “fortunately”! It means you don’t have to worry about where a slice comes from; changes to the original won’t affect the slice. Overall, that makes reasoning about a program’s behavior much easier.

    If you really want to save memory by working with views, consider using numpy arrays. When you slice a numpy array, the memory is shared between the slice and the original:

    >>> a = numpy.arange(3)
    >>> a
    array([0, 1, 2])
    >>> b = a[1:3]
    >>> b
    array([1, 2])
    

    What happens when we modify a and look again at b?

    >>> a[2] = 1001
    >>> b
    array([   1, 1001])
    

    But this means you have to be sure that when you modify one object, you aren’t inadvertently modifying another. That’s the trade-off when you use numpy: less work for the computer, and more work for the programmer!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created the following to explain my problem. Given a list with PayDates
I have the following problem: I am given a tree with N apples, for
I have the following problem, given the following service contract, data contract, and service
Given the following problem , I'd appreciate for any corrections since I have no
I have a problem with nested templates and their template specialization. Given the following
Hi i have following Problem. I write a Mediawiki Extension where i need some
I have the following problem when using java generic. Basically I need to do
I have the following Scala snippet. In order to solve my given problem, I
Given the following problem : we have participants and we want to place them
Consider the following problem: given a list of length three of tuples (String,Int), is

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.