Linked Questions
1,478 questions linked to/from Understanding slicing
91
votes
7
answers
19k
views
Why does Python start at index -1 (as opposed to 0) when indexing a list from the end? [duplicate]
list = ["a", "b", "c", "d"]
print(list[3]) # Number 3 is "d"
print(list[-4]) # Number -4 is "a"
154
votes
3
answers
304k
views
Colon (:) in Python list index [duplicate]
I'm new to Python. I see : used in list indices especially when it's associated with function calls.
Python 2.7 documentation suggests that lists.append translates to a[len(a):] = [x]. Why does one ...
109
votes
2
answers
178k
views
Negative list index? [duplicate]
I'm trying to understand the following piece of code:
# node list
n = []
for i in xrange(1, numnodes + 1):
tmp = session.newobject();
n.append(tmp)
link(n[0], n[-1])
Specifically, I don't ...
70
votes
2
answers
210k
views
What is the meaning of "int(a[::-1])" in Python? [duplicate]
I cannot understand this. I have seen this in people's code. But cannot figure out what it does. This is in Python.
str(int(a[::-1]))
83
votes
2
answers
116k
views
How to get everything from the list except the first element using list slicing [duplicate]
So I have something that I am parsing, however here is an example of what I would like to do:
list = ['A', 'B', 'C']
And using list slicing have it return to me everything but the first index. So in ...
51
votes
4
answers
149k
views
What does [:-1] mean/do in python? [duplicate]
Working on a python assignment and was curious as to what [:-1] means in the context of the following code: instructions = f.readline()[:-1]
Have searched on here on S.O. and on Google but to no ...
33
votes
6
answers
99k
views
What does list[x::y] do? [duplicate]
Possible Duplicate:
Good Primer for Python Slice Notation
I've been reading over some sample code lately and I've read quite a few websites but I just can't seem to get the query right to give me ...
24
votes
5
answers
128k
views
What does :-1 mean in python? [duplicate]
Possible Duplicate:
The Python Slice Notation
I'm trying to port some Python code to C, but I came across this line and I can't figure out what it means:
if message.startswith('<stream:stream'...
16
votes
6
answers
81k
views
Python: What does for x in A[1:] mean? [duplicate]
I was trying to understand Kadane's algorithm from Wikipedia, when I found this:
def max_subarray(A):
max_ending_here = max_so_far = A[0]
for x in A[1:]:
max_ending_here = max(x, ...
30
votes
1
answer
132k
views
What is the meaning of [:] in python [duplicate]
What does the line del taglist[:] do in the code below?
import urllib
from bs4 import BeautifulSoup
taglist=list()
url=raw_input("Enter URL: ")
count=int(raw_input("Enter count:"))
position=int(...
12
votes
1
answer
42k
views
plt.plot meaning of [:,0] and [:,1] [duplicate]
I am plotting a graph using plt.plot using information found online.
However, I do not know what the y[:,0] means:
plt.plot(t, y[:,0], label= 'active Mos')
Similarly, I see y[:,1] a lot too...
...
9
votes
3
answers
100k
views
Extracting a range of data from a python list [duplicate]
I have a list of unicode values. To the best of my knowledge I can use list[starting location:length to select] to select a range of values from a list, right?
I have a list of 78 unicode values, ...
25
votes
2
answers
17k
views
What does extended slice syntax actually do for negative steps? [duplicate]
The extended slice syntax in python has been explained to me as "a[n:m:k] returns every kth element from n to m".
This gives me a good idea what to expect when k is positive. But I'm lost on how to ...
10
votes
2
answers
38k
views
Getting the first x item from a list [duplicate]
Possible Duplicate:
Good Primer for Python Slice Notation
I have a string and I'm splitting in a ; character, I would like to associate this string with variables, but for me just the first x ...
14
votes
1
answer
9k
views
Why does this "[::-1]" return a reversed list in Python? [duplicate]
Possible Duplicate:
Good Primer for Python Slice Notation
reverse a string in Python
I've seen this syntax crop up in a few code snippets I've seen lately, and I'm curious as to what it does. ...