[Python-Dev] extended slicing for lists

Michael Hudson mwh21@cam.ac.uk
29 Jul 2000 11:41:30 +0100


Michael Hudson <mwh21@cam.ac.uk> writes:

> The patch is here, btw:
> 
> http://sourceforge.net/patch/?func=detailpatch&patch_id=100998&group_id=5470

And has now been updated to support assignments & deletions.  I'm
happy with deletions, but assignments seem to be a bit
counter-intuitive - for example both of Guido's examples:

  >>> l = range(10)
  >>> l[2:10:2] = [0]*5
  >>> l
  [0, 1, 0, 3, 0, 5, 0, 7, 0, 9]
  >>> l = range(10)
  >>> l[4:-1:-1] = range(5)
  >>> l
  [4, 3, 2, 1, 0, 5, 6, 7, 8, 9]

are wrong!

  >>> l[2:10:2] = [0]*5
  Traceback (most recent call last):
    File "<stdin>", line 1, in ?
  ValueError: attempt to assign list of size 5 to extended slice of size 4
  >>> l[4:-1:-1] = range(5)
  Traceback (most recent call last):
    File "<stdin>", line 1, in ?
  ValueError: attempt to assign list of size 5 to extended slice of size 0

So I'd really appreciate it if people could play around with the patch
and see if (a) they can break it (b) they like the way assignments
behave (I am already hooked on the indexing & deletion).

now-all-we-need-is-a-slice-expression-for-sorting-ly y'rs
M.

-- 
59. In English every word can be verbed. Would that it were so in
    our programming languages.
  -- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html