Linked Questions

2708 votes
16 answers
4.1m 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 ...
Joan Venge's user avatar
  • 332k
93 votes
7 answers
23k 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"
abraham's user avatar
  • 843
185 votes
7 answers
1.1m 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], ...}...
Keenan's user avatar
  • 2,047
175 votes
3 answers
378k 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 ...
kuriouscoder's user avatar
  • 5,612
105 votes
2 answers
355k 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]))
sofa_maniac's user avatar
  • 1,667
110 votes
2 answers
196k 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 ...
Omid CompSCI's user avatar
  • 1,912
61 votes
4 answers
242k 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 ...
Matt.'s user avatar
  • 1,314
32 votes
9 answers
78k 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] '...
jtbradle's user avatar
  • 2,518
37 votes
6 answers
120k 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 ...
pri0ritize's user avatar
33 votes
5 answers
162k 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 ...
Swen Kooij's user avatar
23 votes
6 answers
134k 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, ...
Margo Eastham's user avatar
41 votes
1 answer
195k 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(...
Sourav's user avatar
  • 543
20 votes
1 answer
67k 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... ...
Hotaru's user avatar
  • 207
14 votes
3 answers
147k 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, ...
Fisher's user avatar
  • 265
29 votes
2 answers
20k 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 ...
Alex Becker's user avatar

15 30 50 per page
1
2 3 4 5
111