What is a negative index in python?

Answer Posted / chaitanya

Python arrays & list items can be accessed with positive or negative numbers (also known as index).

For instance our array/list is of size n, then for positive index 0 is the first index, 1 second, last index will be n-1. For negative index, -n is the first index, -(n-1) second, last negative index will be – 1.

A negative index accesses elements from the end of the list counting backwards.

An example to show negative index in python

>>> import array

>>> a= [1, 2, 3]

>>> print a[-3]

1

>>> print a[-2]

2

>>> print a[-1]

3

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain how memcached should not be used in your python project?

530


What is the use of globals() function in python?

459


Explain about different blocks in exception handling?

446


Please explain in python what is slicing?

439


How do you remove the leading whitespace in a string in python?

480






Explain different ways to create an empty numpy array in python?

505


Give an example of shuffle() method?

455


What is blob in python?

408


What is *args and **kwargs?

482


Is python dictionary a hash table?

422


Tell me what are different methods to copy an object in python?

459


What is means by call function in python?

466


Does python support interfaces like in java?

466


What is the output of this following statement?

465


What is the best ide for python?

452