-1

I would like to know whats the use of [-1] and [0] here. I also tried [1] in the first split and still working the same.

symbols = ["Wiki/ADBE.4", "Wiki/ALGN.4"]

clean_symbols = []

for symbol in symbols:

    symbol = symbol.split("Wiki/")[-1].split(".4")[0]
    print(symbol)
    clean_symbols.append(symbol)

print(clean_symbols)

Thanks!

melpomene
  • 84,125
  • 8
  • 85
  • 148
Lazzal
  • 77
  • 6
  • Possible duplicate of [Understanding Python's slice notation](https://stackoverflow.com/questions/509211/understanding-pythons-slice-notation) – hellow Oct 15 '18 at 09:23
  • 2
    @hellow: this is not *slicing* though – blue_note Oct 15 '18 at 09:23
  • the [-1] means the last one and the [0] means the first one in the string/list of the variable. And it is not a copy @hellow. – LoneWolf Oct 15 '18 at 09:25
  • Have you looked at the duplicate? The OPs question is answered there. Provide a better duplicate, if you insist. – hellow Oct 15 '18 at 09:27
  • no he means what the code is doing @hellow – LoneWolf Oct 15 '18 at 09:27
  • *'whats the use of [-1] and [0] here?'* looks to me, as he does not know, what `-1` is doing there. The duplicate states: `a[-1] # last item in the array` – hellow Oct 15 '18 at 09:28
  • I understand slicing, the thing is that I didnt know how all put together works because : i didnt know split gives a list and I didnt know how that list is placed. Thanks for the help to everybody – Lazzal Oct 15 '18 at 10:00

3 Answers3

1

split creates a list. The rest is just list indexing. Negative index numbers count from the end, so [-1] is the last element of the list created by the first split. The next [0] index means the first element of the list created by the second split (just like it does in almost all languages).

Since [-1] and [1] work the same way, it probably means that your list has exactly 2 elements, so its last (-1) element is the same as its second ([1]).

blue_note
  • 27,712
  • 9
  • 72
  • 90
1

It's just indexing in lists. Let's look at how it works:

>>> symbol = "Wiki/ADBE.4" # this happens in the for loop
>>> symbol.split("Wiki/")
['', 'ADBE.4']

We have got two items in a list, created by split. Lists are indexed from 0, so 1 is "second item" and -1 is "the last item". In this case, this is the same item, so it works for both 1 and -1. But it really works that way only because you have a list with two items:

>>> symbol.split("Wiki/")[-1]
'ADBE.4'
>>> symbol.split("Wiki/")[1]
'ADBE.4'

If you had more, it would not be the same result:

>>> x = ['first', 'second', 'third']
>>> x[-1]
'third'
>>> x[1]
'second'

And then the same thing happens for the new string we got. A list and then an index picking the first item:

>>> symbol.split("Wiki/")[-1].split(".4")
['ADBE', '']
>>> symbol.split("Wiki/")[-1].split(".4")[0]
'ADBE'

And that's all the magic.

Zopper
  • 132
  • 9
0

For first iteration, split returns a list of which we are interested in the last element. Hence [-1]

symbol.split("Wiki/") returns ['', 'ADBE.4']
symbol.split("Wiki/")[-1] returns 'ADBE.4'

Hence, the second split returns a list of which we need the first element, hence [0]

'ADBE.4'.split('.4') returns ['ADBE','']
'ADBE.4'.split('.4')[0] returns 'ADBE'
Shivkumar Birnale
  • 2,906
  • 1
  • 11
  • 17