>>> from operator import itemgetter
>>> a = range(100)
>>> itemgetter(5,13,25)(a)
(5, 13, 25)
Answer from John La Rooy on Stack OverflowI was going through crash course and was learning slicing and i tried
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
it gave me ['charles', 'martina', 'michael']
shouldnt it give me ['michael', 'florence', 'eli']?
Python List Slicing with Arbitrary Indices - Stack Overflow
How to slice a list from an element n to the end in Python? - Stack Overflow
Slicing list of lists in Python - Stack Overflow
python - How does assignment work with list slices? - Stack Overflow
Videos
>>> from operator import itemgetter
>>> a = range(100)
>>> itemgetter(5,13,25)(a)
(5, 13, 25)
If you are a Matlab user, but want to use Python, check out numpy:
In [37]: import numpy as np
In [38]: a = np.arange(100)
In [39]: s = a[[5,13,25]]
In [40]: s
Out[40]: array([ 5, 13, 25])
Here is a comparison of NumPy and Matlab, and here is a table of common Matlab commands and their equivalents in NumPy.
Very rarely using slice objects is easier to read than employing a list comprehension, and this is not one of those cases.
>>> A = [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]
>>> [sublist[:3] for sublist in A]
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
This is very clear. For every sublist in A, give me the list of the first three elements.
With numpy it is very simple - you could just perform the slice:
In [1]: import numpy as np
In [2]: A = np.array([[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]])
In [3]: A[:,:3]
Out[3]:
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
You could, of course, transform numpy.array back to the list:
In [4]: A[:,:3].tolist()
Out[4]: [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
You are confusing two distinct operation that use very similar syntax:
1) slicing:
b = a[0:2]
This makes a copy of the slice of a and assigns it to b.
2) slice assignment:
a[0:2] = b
This replaces the slice of a with the contents of b.
Although the syntax is similar (I imagine by design!), these are two different operations.
When you specify a on the left side of the = operator, you are using Python's normal assignment, which changes the name a in the current context to point to the new value. This does not change the previous value to which a was pointing.
By specifying a[0:2] on the left side of the = operator, you are telling Python you want to use slice assignment. Slice assignment is a special syntax for lists, where you can insert, delete, or replace contents from a list:
Insertion:
>>> a = [1, 2, 3]
>>> a[0:0] = [-3, -2, -1, 0]
>>> a
[-3, -2, -1, 0, 1, 2, 3]
Deletion:
>>> a
[-3, -2, -1, 0, 1, 2, 3]
>>> a[2:4] = []
>>> a
[-3, -2, 1, 2, 3]
Replacement:
>>> a
[-3, -2, 1, 2, 3]
>>> a[:] = [1, 2, 3]
>>> a
[1, 2, 3]
Note:
The length of the slice may be different from the length of the assigned sequence, thus changing the length of the target sequence, if the target sequence allows it. - source
Slice assignment provides similar function to tuple unpacking. For example, a[0:1] = [4, 5] is equivalent to:
# Tuple Unpacking
a[0], a[1] = [4, 5]
With tuple unpacking, you can modify non-sequential lists:
>>> a
[4, 5, 3]
>>> a[-1], a[0] = [7, 3]
>>> a
[3, 5, 7]
However, tuple unpacking is limited to replacement, as you cannot insert or remove elements.
Before and after all these operations, a is the same exact list. Python simply provides nice syntactic sugar to modify a list in-place.