Skip to content

Instantly share code, notes, and snippets.

@zhenyi2697
Created February 24, 2013 10:36
Show Gist options
  • Save zhenyi2697/5023358 to your computer and use it in GitHub Desktop.
Save zhenyi2697/5023358 to your computer and use it in GitHub Desktop.
Python: list slice - insert and clear
>>> # Replace some items:
... a[0:2] = [1, 12]
>>> a
[1, 12, 123, 1234]
>>> # Remove some:
... a[0:2] = []
>>> a
[123, 1234]
>>> # Insert some:
... a[1:1] = ['bletch', 'xyzzy']
>>> a
[123, 'bletch', 'xyzzy', 1234]
>>> # Insert (a copy of) itself at the beginning
>>> a[:0] = a
>>> a
[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
>>> # Clear the list: replace all items with an empty list
>>> a[:] = []
>>> a
[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment