Skip to content
← Back to Community
When using functions in Python, what do [insert integer here] mean?
Profile icon
TorranceNunn

A skeleton was provided to me and in the team1 file, some of the lines read: if (len(function1) < 2):
return "r"
if (function1[-1] == function1[-2]):

What does the [-2] and [-1] mean? Ignore the errors, I just want to know what the brackets mean.

Answered by Roar123 [earned 5 cycles]
View Answer
Voters
Profile icon
SuhruthVarma
Profile icon
TorranceNunn
Comments
hotnewtop
Profile icon
Roar123

First of all, I'm assuming function1 is a list, so this should probably be renamed. Or function1() returns a list, in that case use function1() instead of just the name. For your question, the numbers inside the bracket are indexes and list[index] gives you the value at that index in the list. A negative index starts from the end of the list rather than the beginning, so -1 is the last index and -2 is the 2nd to last index.

Profile icon
TorranceNunn

Oh ok thanks! This is exactly what I needed to know to complete my project! @Roar123

Profile icon
Roar123

@TorranceNunn Glad I could help! If this helped you, select it as the answer.