OKPEDIA PYTHON LISTA EN

How to read an item in the list in Python

To read a list element in Python, write the name of the list indicating the position of the element in square brackets.

namelist[i]

The parameter i is an integer. It is the index that indicates the position of the element in the list starting from 0 (first element).

example python list

Example

Create a list with 4 elements.

>>> color=['red','blue','green','black']

Read the first item (i=0).

>>> print(color[0])
red

Read the second element (i=1)

>>> print(color[1])
blue

Read the third element (i=2)

>>> print(color[2])
green

Read the fourth element (i=3)

>>> print(color[3])
black

If you don't know the index number of the last item in the list, you can also type (i=-1).

>>> print(color[-1])
black

Final result is the same

https://how.okpedia.org/en/python/how-to-read-an-item-in-the-list-in-python


Report an error or share a suggestion to enhance this page


Python List


FacebookTwitterLinkedinLinkedin