Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python negative indexing

myList = [1, 2, 3, 4, 5]

# NORMAL INDEXING
print(myList[0]) # 1
print(myList[1]) # 2
print(myList[2]) # 3

# NEGATIVE INDEXING (STARTS FROM LAST ELEMENT IN LIST)
print(myList[-1]) # 5
print(myList[-2]) # 4
print(myList[-3]) # 3

print(myList[-3:]) # Last 3 elements
Source by www.codegrepper.com #
 
PREVIOUS NEXT
Tagged: #python #negative #indexing
ADD COMMENT
Topic
Name
1+9 =