Created
February 24, 2013 10:36
-
-
Save zhenyi2697/5023358 to your computer and use it in GitHub Desktop.
Python: list slice - insert and clear
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> # 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