slicing is faster than anything in terms of making a copy of an item.
You can easily test this yourself:
timeit.timeit('clone = lst * 1', 'lst = ["foo", "bar"]', number=10000000)
For me, I get a time of 0.8731180049981049
The same test with a slice instead:
timeit.timeit('clone = lst [:]', 'lst = ["foo", "bar"]', number=10000000)
Gives a time of: 0.9454964299984567
So the *
operator seems to actually be faster. As to why people use [:]
instead? It's more obvious as to what its doing.
This breaks down when copying lists of lists, consider:
>>> lst = [[1], [2]]
Copying with a slice and with a *1
still allows for modification of the original list
>>> copy1 = lst[:]
>>> copy1[0].append('x')
>>> lst # is affected by the above
[[1, 'x'], [2]]
>>> copy2 = lst * 1
>>> copy2[0].append('y')
>>> lst # is also affected by the above
[[1, 'x', 'y'], [2]]
But with a list comprehension the sequence can be deep-copied
>>> copy3 = [l[:] for l in lst]
>>> copy3[0].append('z')
>>> lst # unaffected
[[1, 'x', 'y'], [2]]
>>> copy4 = [l * 1 for l in lst]
>>> copy4[0].append('z')
>>> lst # also unaffected
[[1, 'x', 'y'], [2]]