Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List assignment with [:]

Tags:

python

list

What's the difference between

list = range(100)

and

list[:] = range(100)

in Python?

EDIT

I should have mentioned that before that assignment list variable was already assigned to a list:

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

or

list = [1, 2, 3]
list[:] = range(100)
like image 773
Max Avatar asked Oct 06 '11 16:10

Max


People also ask

What does [:] do to a list in Python?

A shortcut way to copy a list or a string is to use the slice operator [:] . This will make a shallow copy of the original list keeping all object references the same in the copied list.

What is list assignment in Python?

Python has a great built-in list type named "list". List literals are written within square brackets [ ]. Lists work similarly to strings -- use the len() function and square brackets [ ] to access data, with the first element at index 0. (See the official python.org list docs.)

What is list assignment index out of range?

The Python "IndexError: list assignment index out of range" occurs when we try to assign a value at an index that doesn't exist in the list. To solve the error, use the append() method to add an item to the end of the list, e.g. my_list. append('b') . Here is an example of how the error occurs. main.py.

How do you assign a variable to a list in Python?

To initialize a list in Python assign one with square brackets, initialize with the list() function, create an empty list with multiplication, or use a list comprehension. The most common way to declare a list in Python is to use square brackets.


1 Answers

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.

like image 185
agf Avatar answered Oct 08 '22 10:10

agf