Copy List Using [:] In Python

The empty slice operator [:] in Python is a powerful and concise way of copying a list.

It is shorthand syntax allowing you to create a new list that contains all the elements of the existing list where the operator is used. This operator is represented by one colon wrapped in square brackets [:] with no values or spaces inside.

While the empty slice operator may seem like a simple and straightforward way to copy a list, it is important to note that it only creates a shallow copy . This means that if the list contains mutable objects, such as other lists or dictionaries, the new list will still reference the objects contained in the original list. Therefore, any changes made to the original objects will also be reflected in the new copied list.

Here’s an example demonstrating this outcome:

>>> list1 = [[1, 2, 3], 'a']
>>> list2 = list1[:]
>>> list1[0].append(4)
>>> list1
[[1, 2, 3, 4], 'a']
>>> list2
[[1, 2, 3, 4], 'a']

As you can see list2 was copied before any edits were applied to list1 . However, as the list copying syntax [:] only performed a shallow copy of the original list ( list1 ) this meant when changes were made to objects already contained in list1 that this would be reflected in all copies of list1 such as list2 .

In essence, the empty slice operator is the equivalent of using the list.copy() function, but it is a shorthand version of it.

Both methods create a new list with the same elements as the original list. However, the list.copy() function is more explicit and easier to understand for those who may not be familiar with the empty slice operator [:] .

How Does the Empty Slice Operator Work?

The empty slice operator is a notation used in Python to create a shallow copy of a list. It is represented by a colon in square brackets [:] with no start or end indices specified on either side of the colon. This operator can be used to create a new list with the same elements as the original list but with a different memory address.

The empty slice operator is a concise and efficient way to copy a list in Python. It is often used when the original list needs to be preserved, and modifications need to be made to the copied list.

Here is an example of how the empty slice operator can be used to create a shallow copy of a list:

>>> original_list = [1, 2, 3, 4, 5]
>>> copied_list = original_list[:]
>>> print(copied_list) 
[1, 2, 3, 4, 5]

Checking List Is A Copy

How can you check the new list is a copy of the original list?

You can check a new list is a copy of the original list by using the == syntax, demonstrated as follows:

>>> original_list = [1, 2, 3]
>>> new_list = original_list[:]
>>> original_list == new_list
True
>>> original_list is new_list
False

With the above results we can see that a new list created using the slice operator created using the empty slice operator [:] creates a new shallow copy. You can also see from the above that the copied list is not the same list as shown using the is comparator. If this stated True then the slice operator would be just copying a reference to the original list’s memory address.

Conclusion

In conclusion, the empty slice operator in Python is a useful shortcut for creating shallow copies of lists. It provides a more concise and readable way to achieve the same result as the list.copy() function. However, it’s important to note that the empty slice operator only creates a shallow copy , meaning that any nested objects within the list will still be references to the original objects.

Overall, the empty slice operator is a handy tool to have in your Python toolkit, but it’s important to understand its limitations and use it appropriately.

By understanding the differences between the empty slice operator [:] and the list.copy() function, developers can choose the most appropriate method for their specific use case and write more efficient and effective code.

Next, you might like to read an article about shallow and deep copies as it pertains to lists .

Photo of author
Ryan Sheehy
Ryan has been dabbling in code since the late '90s when he cut his teeth exploring VBA in Excel. Having his eyes opened with the potential of automating repetitive tasks, he expanded to Python and then moved over to scripting languages such as HTML, CSS, Javascript and PHP.