Get item at specific index in list


Get item at specific index in list

To get item from a list, at specific index, in Python, we can use square bracket notation on the list.

The syntax of the expression to get an item from list x at index i is

x[i]

Examples

1. Get item at index 4 in list x

In the following program, we take a list x, get the value at index 4, and print it to console output.

Python Program

x = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
item = x[4]
print(item)

Output

e

Explanation

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
  0    1    2    3    4    5    6    7   <- indices
                    x[4] = 'e'

Summary

In this tutorial of Python Examples, we learned how to get an item from the list at specified index, with the help of well detailed examples.