18

What's the difference between

lst  = range(100)

and

lst[:] = range(100)

Before that assignment the lst variable was already assigned to a list:

lst = [1, 2, 3]
lst = range(100)

or

lst = [1, 2, 3]
lst[:] = range(100)
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Max
  • 19,654
  • 13
  • 84
  • 122
  • 1
    Also notice that `id(list[:])` is different from `id(list)` and changes with each call. – rplnt Oct 06 '11 at 16:27
  • 1
    @rplnt Slicing on the left side of an assignment is different than on the right side / without assignment; normal slicing making a copy is actually not relevant to this question. – agf Oct 06 '11 at 16:36

6 Answers6

41

When you do

lst = anything

You're pointing the name lst at an object. It doesn't change the old object lst used to point to in any way, though if nothing else pointed to that object its reference count will drop to zero and it will get deleted.

When you do

lst[:] = whatever

You're iterating over whatever, creating an intermediate tuple, and assigning each item of the tuple to an index in the already existing lst object. That means if multiple names point to the same object, you will see the change reflected when you reference any of the names, just as if you use append or extend or any of the other in-place operations.

An example of the difference:

>>> lst = range(1, 4)
>>> id(lst)
74339392
>>> lst = [1, 2, 3]
>>> id(lst)  # different; you pointed lst at a new object
73087936
>>> lst[:] = range(1, 4)
>>> id(lst)  # the same, you iterated over the list returned by range
73087936
>>> lst = xrange(1, 4)
>>> lst
xrange(1, 4)   # not a list, an xrange object
>>> id(lst)   # and different
73955976
>>> lst = [1, 2, 3]
>>> id(lst)    # again different
73105320
>>> lst[:] = xrange(1, 4) # this gets read temporarily into a tuple
>>> id(lst)   # the same, because you iterated over the xrange
73105320
>>> lst    # and still a list
[1, 2, 3]

When it comes to speed, slice assignment is slower. See Python Slice Assignment Memory Usage for more information about its memory usage.

Community
  • 1
  • 1
agf
  • 171,228
  • 44
  • 289
  • 238
  • 1
    Please talk about the time when it matters which one you use - your examples don't show when the two will have different effects other than minor speed differences. i.e. `l = [1,2,3];l2 = l` - now assigning via `l = ` will change only l; `l[:] = ` will cause l2 to change as well, since they're pointing to the same list, which got changed. – Aaron Dufour Oct 06 '11 at 17:25
  • @Aaron I do talk about that; look at the first paragraph of my answer. "You're pointing the name lst at an object. It doesn't change the old object lst used to point to in any way, though if nothing else pointed to that object its reference count will drop to zero and it will get deleted." I added "That means if multiple names point to the same object, you will see the change reflected when you reference any of the names." and a bit of context to the part about slice assignment, so that the inverse is explicit. – agf Oct 06 '11 at 17:28
  • I remember, and I miss, the days when I didn't have to think about the difference between `range` and `xrange`, and where slice assignment (`x[:]`) would never have entered my head. +1 for reminding me of the internal stuff that I keep trying to forget. – Warren P Mar 26 '12 at 17:48
3

The first one redefines the built-in name list to point to some list.

The second fails with TypeError: 'type' object does not support item assignment.

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • 5
    Not if there is already a variable named `list`, which is clearly what he meant. – agf Oct 06 '11 at 16:20
3

list[:] will only work if there is already an object named list that allows slice assignment.

Also, you shouldn't name variables list because there is a built-in named list which is the list type itself.

Michael Hoffman
  • 32,526
  • 7
  • 64
  • 86
  • 1
    There is more to the difference than just one requires the list to already exist. – agf Oct 06 '11 at 16:20
2

list[:] specifies a range within the list, in this case it defines the complete range of the list, i.e. the whole list and changes them. list=range(100), on the other hand, kind of wipes out the original contents of list and sets the new contents.

But try the following:

a=[1,2,3,4]

a[0:2]=[5,6]

a # prints [5,6,3,4]

You see, we changed the first two elements with the assignment. This means, using this notation, you can change several elements in the list once.

phimuemue
  • 34,669
  • 9
  • 84
  • 115
  • That means the same thing I mentioned in my answer. – Griwes Oct 06 '11 at 16:15
  • I understand how slice can be used to changed a part of the list, but I'm curious if there's any difference when operating on the whole list. What's the difference between changing elements and wiping the contents? Is there any performance or memory use difference? – Max Oct 06 '11 at 16:25
  • `list = range(100)` doesn't wipe out the original contents of anything. It just points `list` at something different. – agf Oct 06 '11 at 16:29
2

[:] is also useful to make a deep copy of the list.

def x(l):
    f=l[:]
    g=l
    l.append(8)
    print "l", l
    print "g", g
    print "f", f

l = range(3)

print l
 #[0, 1, 2]

x(l)
 #l [0, 1, 2, 8]
 #g [0, 1, 2, 8]
 #f [0, 1, 2]

print l
#[0, 1, 2, 8]

Modification to l is get reflected in g (because, both point to same list, in fact, both g and l are just names in python), not in f(because, it's a copy of l)

But, in your case, It doesn't make any difference. (Though, I'm not eligible to comment on any memory usage of both methods.)

Edit

h = range(3)
id(h) #141312204 
h[:]=range(3)
id(h) #141312204 
h=range(3)
id(h) #141312588 

list[:] = range(100) updates the list list = range(100) creates new list.

@agf: thanks for pointing my error

Arun Karunagath
  • 1,593
  • 10
  • 24
  • 1
    `[:]` doesn't make a deep copy, it makes a shallow copy. Separately, slice assignment (slicing on the left side of an assignment) is completely different from normal slicing (slicing on the right side of an assignment / without assignment). It doesn't create a copy at all. – agf Oct 06 '11 at 16:38
  • 1
    h = range(3), id(h) #141312204 h[:]=range(3), id(h) #141312204 h=range(3), id(h) #141312588 list[:] = range(100) updates list list = range(100) creates new list. @agf: thanks for pointing my error – Arun Karunagath Oct 06 '11 at 16:39
  • Sure. More specifically, `list[:] = range(100)` creates a new list with no name, and then updates `list` from it. Two lists exist for a while. `list[:] = xrange(100)` creates a new `xrange` iterator and updates the list from it, without intermediate storage. `list = range(100)` creates a new list, points the name `list` at it, then the old list can be deleted if nothing else points at it as it no longer has a name. See my answer for a bit more complete description. – agf Oct 06 '11 at 16:46
1
list[:] = range(100)

won't work on uninitialized variable, as it is modifying it. The [:] specifies the whole list/touple.

Griwes
  • 8,805
  • 2
  • 43
  • 70