Negative numbers mean that you count from the right instead of the left. So, list[-1] refers to the last element, list[-2] is the second-last, and so on.

Answer from Toomai on Stack Overflow
🌐
W3Schools
w3schools.com › python › ref_list_index.asp
Python List index() Method
Remove List Duplicates Reverse ... Plan Python Interview Q&A Python Training ... The index() method returns the position at the first occurrence of the specified value....
Discussions

Embarrassingly, i don't understand how list indexing works
Let us step back a little. Before understanding your example, try to grok the example below for i in range(0, 3): print(i) which results in: 0 1 2 And the example below my_list = [42, 6, 2024] for i in range(0, 3): print(i, my_list[i]) Which results in: 0 42 1 6 2 2024 Are those clear to ya? Thanks! More on reddit.com
🌐 r/learnpython
26
15
August 31, 2024
python - Can lists start at index 1? - Stack Overflow
I've downloaded from a supposedly serious source a sage script. It doesn't work on my computer, and a quick debugging showed that a problem came from the fact that at some point, the authors were d... More on stackoverflow.com
🌐 stackoverflow.com
What's the meaning and purpose of [-1] in this function?
Well, let's break it down! words = [[str[0]]] This is written somewhat strangely, but basically it is setting the words variable to a list containing the first character of the string (note: this code should not use str as a variable name as this is also a type). for c in str[1:]: This for loop is going over the rest of the string character by character. This is called a slice, and essentially it cuts the list into index values. For example, if you had a list [1, 2, 3, 4, 5] and you indexed it with [2:3], the list you'd get back is [3, 4]. This is because you are slicing from index 2 (the third item since indexes start at 0) to index 3 (the 4th item). If you did [2:] instead, your list would be [3, 4, 5], as the empty value means "to the end of the list." If you reversed it with [:2] you'd instead get a list of [1, 2] because you are slicing from 0 (start) to 2. Note that [:2] and [0:2] are functionally the same. In the next section, you have words[-1][-1]. An index of [-1] means "the last element (length - 1). Any time you have a negative index in Python, it means "that many from the length." NOTE: a slice at the end must be blank if you want the last character. If you did [2:] and [2:-1] you'd get different values...in this case [3, 4, 5] and [3, 4] respectively. So what does the words[-1][-1] actually do? Well, remember that words is a list of characters. So you are looking at the last string and last letter of that string, then checking if it is lower case. The if also checks if the next letter is upper case. If it is, we append a new list of characters to the end of the words list. If not, we are instead appending the current character to the current list. This is repeated for the whole string. This list of lists is then converted into a list of individual strings by combining the internal lists into strings instead of a list of characters. You can tell what's going on if you put a print statement before the return. If I had a string, say, testWordOutput, the list before the returning list comprehension looks like this: [['t', 'e', 's', 't'], ['W', 'o', 'r', 'd'], ['O', 'u', 't', 'p', 'u', 't']]. This is actually a somewhat overcomplicated way to do this. You can skip the entire part where you create the internal lists, for example this code: def split_camel_case(original): if not original: # Early return for empty string return [] words = [original[0]] # Start with the first character for char in original[1:]: # If the current character is uppercase and # the previous character is lowercase, # start a new word if char.isupper() and words[-1][-1].islower(): words.append(char) else: # Otherwise, append the current character to the last word words[-1] += char return words This code does the exact same thing but is perhaps a bit more clear in how it works. It still has many of the same elements (the basic concept wasn't wrong) but removes the need for a list comprehension. Hopefully that makes sense, please let me know if you have questions. More on reddit.com
🌐 r/learnpython
7
3
February 29, 2024
Looking for a way to add an element into a list without using insert()
More time efficient than insert? why are you looking for such a thing? also why do you think such a thing exists? More on reddit.com
🌐 r/learnpython
13
2
January 18, 2020
🌐
ReqBin
reqbin.com › code › python › h54arbqc › python-list-index-example
How do I find the index of an element in a Python list?
To find the index of an element in a Python list, you can use the list.index(element, start, end) method. The list.index() method takes an element as an argument and returns the index of the first occurrence of the matching element.
🌐
Reddit
reddit.com › r/learnpython › embarrassingly, i don't understand how list indexing works
r/learnpython on Reddit: Embarrassingly, i don't understand how list indexing works
August 31, 2024 -

I'm learning data structure and algorithms and i came across a question in the list section. i thought after i had understood python, i had understood list but a question was asked and i find myself finding it hard to understand how the list indexing works.

here's the sample code:

arr = [1, 2, 3, 4, 5, 6]

for i in range(1, 6):

arr[i - 1] = arr[i]

for i in range(0, 6):

print(arr[i], end = " ")

it looks simple to understand but, i just can't understand it.

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-index
Python List index() - Find Index of Item - GeeksforGeeks
Explanation: a.index("dog") searches for "dog" in the list and element is found at index 1, so 1 is returned.
Published: July 17, 2026
🌐
HackerEarth
hackerearth.com › practice › notes › samarthbhargav › a-quick-intro-to-indexing-in-python
A Quick intro to Indexing in Python - Samarth Bhargav
python lists are 0-indexed. So the first element is 0, second is 1, so on. So if the there are n elements in a list, the last element is n-1.
🌐
Purple Frog Systems
purplefrogsystems.com › home › python lists – what do i need to know?
Python Lists – What do I need to know? - Purple Frog Systems
July 22, 2025 - Access elements of a list using list[index]. Indexing in Python starts at 0, which means that the first element has an index of 0.
Find elsewhere
🌐
Programiz
programiz.com › python-programming › methods › list › index
Python List index() (with Code Visualization)
The index() method returns the index of a specified item in the list. If there are multiple matching items, it returns the index of the first occurrence. ... models = ['Claude', 'ChatGPT', 'Gemini', 'ChatGPT'] index = models.index('Gemini') print(index) # Output: 2 index = models.index('ChatGPT') ...
🌐
Roberto Reif
robertoreif.com › blog › 2025 › 7 › 8 › python-lists-index
PYTHON LISTS: Index — Roberto Reif
October 25, 2025 - To do this, use the index method, passing the desired item as an argument. This method returns the index position of the element within the list. What other Python questions do you have
🌐
Medium
medium.com › @tuenguyends › pythons-list-type-part-1-indexing-and-slicing-23ad68ca4c66
Python’s list type (part 1) — Indexing and slicing | by Tue Nguyen | Medium
April 15, 2022 - Each element can be access using its position (or index) in the sequence. Python starts indexing at 0, thus if a sequence has N elements, then the elements are indexed by 0, 1, ..., N-1.
🌐
DataCamp
datacamp.com › tutorial › python-list-index
Python List index() Method Explained with Examples | DataCamp
March 28, 2025 - Python's built-in index() function is a useful tool for finding the index of a specific element in a sequence. This function takes an argument representing the value to search for and returns the index of the first occurrence of that value in the sequence. If the value is not found in the sequence, ...
🌐
Codefinity
codefinity.com › courses › v2 › 102a5c09-d0fd-4d74-b116-a7f25cb8d9fe › 39cc7383-2374-4f3f-b322-2cb0109e6427 › df1b5ff1-bf08-4631-a38b-71e45b0101c0
Learn List Indexing in Python | Mastering Python Lists
In Python, lists allow you to access individual elements using their index. Indexing starts at 0, meaning the first element in a list is at index 0, the second element is at index 1, and so on. This is called zero indexing.
🌐
Reddit
reddit.com › r/learnpython › what's the meaning and purpose of [-1] in this function?
r/learnpython on Reddit: What's the meaning and purpose of [-1] in this function?
February 29, 2024 -

Basically, the purpose of this code is to break up strings that have camelCases into different strings, and I looked up some code samples online, and I found this solution. The code works, however, I don't quite understand what some of the syntax actually does, even after some research. I made a # comment on each line I didn't understand, explaining my confusion.

I simply just don't want to mindlessly copy code off the internet without understanding what it does!

Also, if possible, are there any lessons online you recommend I goto to learn about this concept to avoid further confusion? Thanks!

def camelCase(str):

words = [[str[0]]]

for c in str[1: ]: #What does str[1: ] do? I tried looking it up but Google isn't helpful

if words[-1][-1].islower() and c.isupper(): #I'm not quite sure what function [-1][-1] plays in this function

words.append(list(c))

else:

words[-1].append(c) #I once again don't understand the function of [-1] in this else statement

return [' '.join(word) for word in words]

Top answer
1 of 4
14
Well, let's break it down! words = [[str[0]]] This is written somewhat strangely, but basically it is setting the words variable to a list containing the first character of the string (note: this code should not use str as a variable name as this is also a type). for c in str[1:]: This for loop is going over the rest of the string character by character. This is called a slice, and essentially it cuts the list into index values. For example, if you had a list [1, 2, 3, 4, 5] and you indexed it with [2:3], the list you'd get back is [3, 4]. This is because you are slicing from index 2 (the third item since indexes start at 0) to index 3 (the 4th item). If you did [2:] instead, your list would be [3, 4, 5], as the empty value means "to the end of the list." If you reversed it with [:2] you'd instead get a list of [1, 2] because you are slicing from 0 (start) to 2. Note that [:2] and [0:2] are functionally the same. In the next section, you have words[-1][-1]. An index of [-1] means "the last element (length - 1). Any time you have a negative index in Python, it means "that many from the length." NOTE: a slice at the end must be blank if you want the last character. If you did [2:] and [2:-1] you'd get different values...in this case [3, 4, 5] and [3, 4] respectively. So what does the words[-1][-1] actually do? Well, remember that words is a list of characters. So you are looking at the last string and last letter of that string, then checking if it is lower case. The if also checks if the next letter is upper case. If it is, we append a new list of characters to the end of the words list. If not, we are instead appending the current character to the current list. This is repeated for the whole string. This list of lists is then converted into a list of individual strings by combining the internal lists into strings instead of a list of characters. You can tell what's going on if you put a print statement before the return. If I had a string, say, testWordOutput, the list before the returning list comprehension looks like this: [['t', 'e', 's', 't'], ['W', 'o', 'r', 'd'], ['O', 'u', 't', 'p', 'u', 't']]. This is actually a somewhat overcomplicated way to do this. You can skip the entire part where you create the internal lists, for example this code: def split_camel_case(original): if not original: # Early return for empty string return [] words = [original[0]] # Start with the first character for char in original[1:]: # If the current character is uppercase and # the previous character is lowercase, # start a new word if char.isupper() and words[-1][-1].islower(): words.append(char) else: # Otherwise, append the current character to the last word words[-1] += char return words This code does the exact same thing but is perhaps a bit more clear in how it works. It still has many of the same elements (the basic concept wasn't wrong) but removes the need for a list comprehension. Hopefully that makes sense, please let me know if you have questions.
2 of 4
11
You should experiment with code in the Python shell, that way you can try these things and see what they do.
🌐
Kodeclik
kodeclik.com › what-does-minus-one-index-mean-in-python
Python list[-1]
October 16, 2024 - Python list indices are typically from 0 to 1 less than the length of the list. They can also index from the end of the list, beginning with -1.
🌐
Railsware
railsware.com › home › engineering › indexing and slicing for lists, tuples, strings, other sequential types in python
Python Indexing and Slicing for Lists, Tuples, Strings, other Sequential Types | Railsware Blog
January 22, 2025 - So, we start deletion from 20(index 1) and remove each 2-nd element till the value 80(index 7). And because slice deletion mutates the underlying object, it’s not applicable to immutable sequential types. We discussed two key list operations: indexing and slicing. Both concepts are crucial to efficient Python ...
🌐
Temp Mail
tempmail.us.com › temp mail › blog › python › locating an item's index in a python list
Locating an Item's Index in a Python List - Temp Mail
July 24, 2024 - For example, using [i for i, x in enumerate(my_list) if x == item] will yield a list of all indexes where the item is located if a list contains duplicates of the item. For such use cases, this approach is not only very readable and efficient, but also succinct. Using the numpy library is another sophisticated strategy that works well with massive datasets and numerical calculations. In comparison to native Python lists, numpy provides the np.where() function, which can be used to find indexes more quickly.
🌐
Tutorialspoint
tutorialspoint.com › python › list_index.htm
Python List index() Method
The following example shows the usage of the Python List index() method. aList = [123, 'xyz', 'zara', 'abc']; print("Index for xyz : ", aList.index( 'xyz' )) print("Index for zara : ", aList.index( 'zara' )) When we run above program, it produces following result − · Index for xyz : 1 Index for zara : 2 ·
🌐
Guru99
guru99.com › home › python › python list index() with example
Python List index() with Example
July 11, 2026 - The value returned by index() is always a non-negative position counted from the start. You may pass negative start or end arguments to bound the search, but index() itself never returns a negative index for a found element. 🔡 Is the Python list index() method case-sensitive?