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
🌐
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.

🌐
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.
🌐
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 - In other words, -1 is the index of the last element. Similarly, -2 is the index of the second-to-last element.
🌐
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 - Or the penultimate element? In this case, we want to enumerate elements from the tail of a list. To address this requirement there is negative indexing. So, instead of using indexes from zero and above, we can use indexes from -1 and below.
🌐
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
🌐
Python
docs.python.org › 2.7 › tutorial › datastructures.html
5. Data Structures — Python 2.7.18 documentation
Return the index in the list of the first item whose value is x. It is an error if there is no such item. ... Return the number of times x appears in the list. ... Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation). ... Reverse the elements of the list, in place. ... >>> a = [66.25, 333, 333, 1, 1234.5] >>> print a.count(333), a.count(66.25), a.count('x') 2 1 0 >>> a.insert(2, -1) >>> a.append(333) >>> a [66.25, 333, -1, 333, 1, 1234.5, 333] >>> a.index(333) 1 >>> a.remove(333) >>> a [66.25, -1, 333, 1, 1234.5, 333] >>> a.reverse() >>> a [333, 1234.5, 1, 333, -1, 66.25] >>> a.sort() >>> a [-1, 1, 66.25, 333, 333, 1234.5] >>> a.pop() 1234.5 >>> a [-1, 1, 66.25, 333, 333]
🌐
Python Guides
pythonguides.com › python-array-index-1
How To Use Python Array Index -1?
March 19, 2025 - In a recent Python webinar, array index -1 was the topic of the discussion. We will use index -1 to fetch the last element of the given collection without knowing the length of the collection. Let us learn more about this topic. ... Negative indexing is useful in Python lists.
🌐
ReqBin
reqbin.com › code › python › h54arbqc › python-list-index-example
How do I find the index of an element in a Python list?
In Python, list indexes start at 0. You can also check if an element exists in a list using the "in" operator. In this Python List Index example, we get the index of a list item using the list.index() method.
🌐
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....
🌐
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
Remember that the index corresponds to the position minus one (n - 1). You can also assign a list element to a variable using the assignment operator =, just like you would with any other value: 12 my_favorite_city = cities[0] print(my_favo...
🌐
DataCamp
datacamp.com › tutorial › python-list-index
Python List index() Method Explained with Examples | DataCamp
March 28, 2025 - In Python, indexing refers to the process of accessing a specific element in a sequence, such as a string or list, using its position or index number. Indexing in Python starts at 0, which means that the first element in a sequence has an index of 0, the second element has an index of 1, and so on.
🌐
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.
🌐
Face Surgery
face.meei.harvard.edu › home › bestof › index of list python › python list index() method (with examples) - scaler topics
Python List index() Method (With Examples) - Scaler Topics - Face Surgery
August 15, 2025 - Master list indexing in Python. Learn how to access elements, handle negative indexes, and avoid IndexError with this concise, beginner-friendly guide.
🌐
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
🌐
Programiz
programiz.com › python-programming › methods › list › index
Python List index() (with Code Visualization)
models = ['Claude', 'ChatGPT', 'Gemini', 'ChatGPT'] # Search 'ChatGPT' from start to end index = models.index('ChatGPT') print(index) # Output: 1 # Search 'ChatGPT' from index 2 to end index = models.index('ChatGPT', 2) print(index) # Output: 3 # Search 'ChatGPT' from index 2 to index 3 (exclusive) index = models.index('ChatGPT', 2, 3) print(index) # ValueError: 'ChatGPT' is not in list · Note: Python also supports negative indexing and you can use negative start and end indices with index().
🌐
Mimo
mimo.org › glossary › python › index-element
Python Index: Efficient Data Retrieval and Navigation
Python also supports negative indices. A negative index counts from the end of the sequence: ... Get a particular item from a list, tuple, or string using its position. ... Change values in a list by assigning a new value to a specific index.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Find the Index of an Item in a List in Python | note.nkmk.me
July 27, 2023 - Built-in Types - Common Sequence Operations — Python 3.11.4 documentation · Contents · How to use the index() method of a list · Implement a function like the find() method (returns -1 for non-existent values) Get all indices of duplicate items · Specify the search range for the index() method ·