Linked Questions
1,654 questions linked to/from How slicing in Python works
2651
votes
16
answers
3.8m
views
How do I get a substring of a string in Python? [duplicate]
I want to get a new string from the third character to the end of the string, e.g. myString[2:end]. If omitting the second part means 'to the end', and if you omit the first part, does it start from ...
92
votes
7
answers
21k
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"
170
votes
7
answers
930k
views
How to overcome TypeError: unhashable type: 'list' [duplicate]
I'm trying to take a file that looks like this:
AAA x 111
AAB x 111
AAA x 112
AAC x 123
...
And use a dictionary so that the output looks like this
{AAA: ['111', '112'], AAB: ['111'], AAC: [123], ...}...
167
votes
3
answers
349k
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 ...
118
votes
2
answers
201k
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 ...
90
votes
2
answers
304k
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]))
106
votes
2
answers
166k
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 ...
60
votes
4
answers
203k
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 ...
31
votes
9
answers
69k
views
I don't understand slicing with negative bounds in Python. How is this supposed to work? [duplicate]
I am a newbie to Python and have come across the following example in my book that is not explained very well. Here is my print out from the interpreter:
>>> s = 'spam'
>>> s[:-1]
'...
34
votes
6
answers
111k
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 ...
32
votes
5
answers
152k
views
What does :-1 mean in python? [duplicate]
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'):
message = message[:-1] + ' />'
I ...
20
votes
6
answers
119k
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, ...
39
votes
1
answer
175k
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(...
17
votes
1
answer
61k
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...
...
13
votes
3
answers
129k
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, ...