What does this notation do for lists in Python: "someList[:]"?

Question:

I sometimes get across this way of printing or returning a list – someList[:].
I don’t see why people use it, as it returns the full list.

Why not simply write someList, whithout the [:] part?

Asked By: Petr S

||

Answers:

To create a copy of a list instead of passing by reference, as Python does. Use next two example to understand the difference.

Example:

# Passing by reference
SomeListA = [1, 2, 3]
SomeListB = [2, 3, 4]
SomeListB = SomeListA
SomeListA[2] = 5
print SomeListB
print SomeListA

# Using slice
SomeListA = [1, 2, 3]
SomeListB = [2, 3, 4]
SomeListB = SomeListA[:]
SomeListA[2] = 5
print SomeListB
print SomeListA
Answered By: Neithrik

[:] creates a slice, usually used to get just a part of a list. Without any minimum/maximum index given, it creates a copy of the entire list. Here’s a Python session demonstrating it:

>>> a = [1,2,3]
>>> b1 = a
>>> b2 = a[:]
>>> b1.append(50)
>>> b2.append(51)
>>> a
[1, 2, 3, 50]
>>> b1
[1, 2, 3, 50]
>>> b2
[1, 2, 3, 51]

Note how appending to b1 also appended the value to a. Appending to b2 however did not modify a, i.e. b2 is a copy.

Answered By: Frerich Raabe

When you need to modify the list and you don’t want to change the list and create another list you use

y = [1,2,3]
x = y[:]

and you can do a lot of changes to the list but the origin list will be in (y) and the modified in (x)

Answered By: Mohamed Ramzy Helmy

In python, when you do a = b, a doesn’t take the value of b, but references the same value referenced by b. To see this, make:

>>> a = {'Test': 42}
>>> b = a
>>> b['Test'] = 24

What is now the value of a?

>>> a['Test']
24

It’s similar with lists, so we must find a way to really copy a list, and not make a reference to it. One way could be to recreate the list copy = list(list1), or use the functions of the copy module. But, after all, the easiest way, the prettiest, the best way ( 😉 ) for doing this, is to copy each value of the first list to the other, by doing copy = list1[:]. It uses the slices, here list1 is sliced from index 0 to index len(list1), so the whole list1 is returned!

Moreover, the slice method is slightly faster: using the time.clock() method to measure the mean execution time of 1000 assignment of lists, each one containing 10000 random integers, with slices, constructor and deepcopy, the results show that the slices are 15% faster than the constructor method, and deepcopy is 4 times slower. However, this gain of time is negligible while using small lists: thus, using copy = list(list_to_copy) or copy = list_to_copy[:] is up to the developer’s preferences.

Finally, we often forget the list.copy method, which seems to be the faster! In fact, it’s even 13% faster than the slice method!

Answered By: Spirine

There are 2 copies available. 1) Deep Copy 2) Shallow Copy.
1) Deep Copy is you just copy the values
list = ['abc',123,'xyz']
list1 = copy.deepcopy(list) or list1 = list[:]

2) Shallow Copy is you just reference to the varible
list2 = copy.copy(list) or list2 = list

When you modify something on list2 it get effected in list also as it is referenced.
list1.append(456)
list2.append('789')
print "list: %s" %list
print "list1: %s" %list1
print "list2: %s" %list2
ans:
list : ['abc',123,'xyz','789']
list1 : ['abc',123,'xyz',456]
list2 : ['abc',123,'xyz','789']

Answered By: Kit

#Here is a simpler way for beginners to understand:

list16 = [1,2,3,4,5,6]
list17 = list16[:] 

#^Identifying that ‘list17’ is a copy of ‘list16’ and not list16 directly

list17[0] = 10

#^ Making an alteration in the new copied list

print(list17)
print(list16)
= [10,2,3,4,5,6]
= [1,2,3,4,5,6]

#Printing the lists so that you can see what is happening. I created a copy of the list and altered it without changing the original list at all.

Answered By: LORD_OF_SHIT_CODE