Skip to main content

Python List value by Index

Python has the List data type to store the multiple values. While we are creating a List variable, a Python List object is created for storing the list values. We can get individual list value by its index.

Values inside the list are also defined as items. In order to, the list items are seperated with comma (,) within the square bracket([]) on List python data type.

The Python tutorial on Techtuts provides an information about Python and Python 3 from the basis to high level. This tutorial provides a good understanding about the Python and Python for Technical, Non-Technical People, College Students and Working Professionals. Given examples are an easy to write and simple to practice by yourself.

In this tutorial you will learn about the  Python and Python3. This tutorial includes  an Introduction, Python Environment Setup, Variables, Data types , Control flow Statements, Loops, Functions, Data Structures, Modules ,Files, Classes, Python Web Frameworks etc.

Get List values by index

We can get individual list value by its index. Index should be negative or positive. But it should be within length of List values.

Positive index

Positive index starts with 0 and  goes up.

Example

>>> listVar = ['get','net', 'pet', 'set']
>>> listVar
['get', 'net', 'pet', 'set']
>>> listVar[2]
'pet'
>>>

Negative index

we can also use negeative index.  -1 refers  to the last index. -2 refers to  second value from the last values

Example

>>> listVar =['get','net','pet','set']
>>> listVar
['get', 'net', 'pet', 'set']
>>> listVar[-1]
'set'
>>> listVar[-2]
'pet'
>>>