How to Use List Access Method in PythonPython Basics


The function of the operator [] is used to access the values in the list.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

list1 = [0,1, 2, 3, 4, 5, 6, 7]

print("list1[0]: ", list1[0])
print("list2[1:5]: ", list1[0:5])

Output:

list1[0]:  0
list2[1:5]:  [0, 1, 2, 3, 4]

Syntax

list[n]

Parameters

Name Description
n The index of the element to be accessed.

Return Value

It returns the corresponding value of the element at index n.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us