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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:52:01+00:00 2026-05-24T07:52:01+00:00

As I only now noticed after commenting on this answer , slices in Python

  • 0

As I only now noticed after commenting on this answer, slices in Python 3 return shallow copies of whatever they’re slicing rather than views. Why is this still the case? Even leaving aside numpy’s usage of views rather than copies for slicing, the fact that dict.keys, dict.values, and dict.items all return views in Python 3, and that there are many other aspects of Python 3 geared towards greater use of iterators, makes it seem that there would have been a movement towards slices becoming similar. itertools does have an islice function that makes iterative slices, but that’s more limited than normal slicing and does not provide view functionality along the lines of dict.keys or dict.values.

As well, the fact that you can use assignment to slices to modify the original list, but slices are themselves copies and not views, is a contradictory aspect of the language and seems like it violates several of the principles illustrated in the Zen of Python.

That is, the fact you can do

>>> a = [1, 2, 3, 4, 5]
>>> a[::2] = [0, 0, 0]
>>> a
[0, 2, 0, 4, 0]

But not

>>> a = [1, 2, 3, 4, 5]
>>> a[::2][0] = 0
>>> a
[0, 2, 3, 4, 5]

or something like

>>> a = [1, 2, 3, 4, 5]
>>> b = a[::2]
>>> b
view(a[::2] -> [1, 3, 5])   # numpy doesn't explicitly state that its slices are views, but it would probably be a good idea to do it in some way for regular Python
>>> b[0] = 0
>>> b
view(a[::2] -> [0, 3, 5])
>>> a
[0, 2, 3, 4, 5]

Seems somewhat arbitrary/undesirable.

I’m aware of http://www.python.org/dev/peps/pep-3099/ and the part where it says “Slices and extended slices won’t go away (even if the __getslice__ and __setslice__ APIs may be replaced) nor will they return views for the standard object types.”, but the linked discussion provides no mention of why the decision about slicing with views was made; in fact, the majority of the comments on that specific suggestion out of the suggestions listed in the original post seemed to be positive.

What prevented something like this from being implemented in Python 3.0, which was specifically designed to not be strictly backwards-compatible with Python 2.x and thus would have been the best time to implement such a change in design, and is there anything that may prevent it in future versions of Python?

  • 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-24T07:52:02+00:00Added an answer on May 24, 2026 at 7:52 am

    Well it seems I found a lot of the reasoning behind the views decision, going by the thread starting with http://mail.python.org/pipermail/python-3000/2006-August/003224.html (it’s primarily about slicing strings, but at least one e-mail in the thread mentions mutable objects like lists), and also some things from:

    http://mail.python.org/pipermail/python-3000/2007-February/005739.html
    http://mail.python.org/pipermail/python-dev/2008-May/079692.html and following e-mails in the thread

    Looks like the advantages of switching to this style for base Python would be vastly outweighed by the induced complexity and various undesirable edge cases. Oh well.

    …And as I then started wondering about the possibility of just replacing the current way slice objects are worked with with an iterable form a la itertools.islice, just as zip, map, etc. all return iterables instead of lists in Python 3, I started realizing all the unexpected behavior and possible problems that could come out of that. Looks like this might be a dead end for now.

    On the plus side, numpy’s arrays are fairly flexible, so in situations where this sort of thing might be necessary, it wouldn’t be too hard to use one-dimensional ndarrays instead of lists. However, it seems ndarrays don’t support using slicing to insert additional items within arrays, as happens with Python lists:

    >>> a = [0, 0]
    >>> a[:1] = [2, 3]
    >>> a
    [2, 3, 0]
    

    I think the numpy equivalent would instead be something like this:

    >>> a = np.array([0, 0])  # or a = np.zeros([2]), but that's not important here
    >>> a = np.hstack(([2, 3], a[1:]))
    >>> a
    array([2, 3, 0])
    

    A slightly more complicated case:

    >>> a = [1, 2, 3, 4]
    >>> a[1:3] = [0, 0, 0]
    >>> a
    [1, 0, 0, 0, 4]
    

    versus

    >>> a = np.array([1, 2, 3, 4])
    >>> a = np.hstack((a[:1], [0, 0, 0], a[3:]))
    >>> a
    array([1, 0, 0, 0, 4])
    

    And, of course, the above numpy examples don’t store the result in the original array as happens with the regular Python list expansion.

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

Sidebar

Related Questions

Working in Delphi7 just now, I noticed that not only a VarIsEmpty function exists,
I've needed this a few times, and only now it occured to me, that
Java SE 6 (64 bit only) is now on OS X and that is
it appears that the Apple site now only provides download for Xcode 3.2, that
I am working on an in-place HTML editor, concentrating on Firefox only right now.
I've got an app that's in invite-only beta right now. Problem is, I can't
I'm building a PHP site, but for now the only PHP I'm using is
We have a website; which, till now had only HTML pages. Now we are
I'm trying to implement language switching in .htaccess, and the only thing left now
I've got a MySQL database that stores image information. Right now it only has

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.