0

I am basically a PHP programmer, and I'm trying to interpret a simple line in Python:

thekey = data['k'][data['k'].index(':') + 1:]

I am getting confused about the + 1:. the data['k'].index(':') in php is array_search of ':', but I am getting confused about the + 1:

b4hand
  • 9,550
  • 4
  • 44
  • 49
user1217974
  • 154
  • 3
  • 12
  • We need more information. What is stored in `data`? – mdml Nov 01 '14 at 21:45
  • plz share the input too – Hackaholic Nov 01 '14 at 21:46
  • Are you sure that's right? Doing both `data['k']` and `data[...:]` is unusual. Is it perhaps supposed to be `data['k'][data['k'].index(':') + 1:]`? – BrenBarn Nov 01 '14 at 21:52
  • @BrenBarn yea mate thats rite .... – user1217974 Nov 01 '14 at 21:54
  • @user1217974: I edited your question to replace it. If you made a mistake posting the code, there's no need to leave the old, incorrect version in; just replace it with the correct one. – BrenBarn Nov 01 '14 at 21:56
  • @BrenBarn Uh, so now that the code is understood by the OP...why are you editing it into the original post. Throws off all 3 of the current answers and serves no benefit. – phantom Nov 01 '14 at 21:57
  • @phantom: Sorry, I pasted the wrong thing (pasted my reformatting rather than the OP's correction). The OP posted the wrong code initially. It's better to have the question show the correct code, instead of showing incorrect code with the correct code separately in an "edit" at the bottom. The answers are still essentially right. – BrenBarn Nov 01 '14 at 21:59
  • The code should probably instead be something like `thekey = data['k'].partition(':')[2]`. – Karl Knechtel Nov 01 '14 at 22:00

3 Answers3

2

It means that first you search for the first occurence of : using the .index () method, and then make a slice of the list starting from the position right after that occurence up to the end of the list.

Maciej Gol
  • 15,394
  • 4
  • 33
  • 51
2

Yes this is a fairly confusing bit of code.

Splice syntax looks like this:

[start:end]

When end is left blank, it grabs everything until the end of the data, as in this case.

The + 1 is merely adding one to the result of the data['k'].index(':') call, the colon : at the end is the splice operator. This could be expanded like this for readability:

x = data['k'].index(':')
thekey = data['k'][x+1 : ]

As the comments said, it would really be easier to answer this question if you gave us the contents of data.

Further reading.

Community
  • 1
  • 1
phantom
  • 1,457
  • 7
  • 15
1

+ 1: is not a unit here. + 1 is part of the slice start, and : is what separates the start from the end. In Python, some_list[start:end] means "elements of some_list starting at (and including) start and up to (but not including) end". some_list[start:] means "elements starting from start and going all the way to the end of the list".

You may get a better understanding by thinking of the code this way:

thekey = data['k'][ (data['k'].index(':')+1) : ]

the entire bit data['k'].index(':')+1 is an expression giving the start of the slice (in this case, the position right after the first colon in data['k']). So this would mean "give me all of data['k'] after the first colon".

BrenBarn
  • 242,874
  • 37
  • 412
  • 384