Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python list slice examples
# Python's list slice syntax can be used without indices
# for a few fun and useful things:
lst = [1, 2, 3, 4, 5]
del lst[:]
# >>> []
# You can replace all elements of the list
# without creating a new list object:
a = lst
list[:] = [7, 8, 9]
# >>> lst
# >>> [7, 8, 9]
# >>? a
# >>> [7, 8, 9]
a is lst
# >>> True
# You can also create a (shallow) copy of a list:
b = lst[:]
# >>> b
# >>> b
# >>> [7, 8, 9]
b is lst
# >>> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.