How to access element of List Python

In this post, we will learn How to access element of List Python with examples.The list is a sequence and ordered collection data type in Python that is represented by square brackets([]) and each item in the list is separated by a comma (,) and all list elements are indexed

How to Access elements of list Python


There are three ways to access elements of the list.

  • List Posting Indexing
  • Negative Indexing
  • Range of index

1 How to Access element of List Python using List Posting Indexing


Python List element is accessed by an index by providing the index of the element in square brackets([]). The index of the list elements starts from 0 and length-1. A list of n elements will have an index range of start index is 0 and the last index is n-1.

Syntax

listname[index]

Important Note:

  • The index must be an integer type, it can not be afloat.
  • To avoid IndexError (out of range) pass index in range, A list has 7 elements, has index range will be 0 to 6, accessing index except these indexes will throw IndexError: out of range exception.
pre_List = ['at','on','into','up','The']

print('Accessing single list:')
print(pre_List[1])
print(pre_List[3])
print(pre_List[4])





print('\n Accessing element of  nested list:')
nested_list = list((["at", "up",20,30,2.5,4.5],[12,34,16])) 
print(nested_list[0][1])
print(nested_list[0][2])
print(nested_list[0][3])
print(nested_list[1][1])

#passing an index of float type

print(pre_List[1.3])

Output

Accessing single list:
on
up
The


Accessing element of nested list:
up
20
30
34
Traceback (most recent call last):
  File "main.py", line 27, in <module>
    print(pre_List[1.3])
TypeError: list indices must be integers or slices, not float

2.How to Access element of List Python using Negative Indexing


The list element is accessed by providing the Negative index in the square bracket([]) the negative index starts from the end. So -1 is for the first last element, -2 for the second last element, and so on till the list length.

#Accessing single list element by negative Index

pre_List = ['at','on','into','up','The']


print('Accessing single list element by negative Index:')
print(Access the nth element,'pre_List[-1])
print(pre_List[-2])
print(pre_List[-3])
print(pre_List[-4])
print(pre_List[-5])





#Accessing nested list by negative Index

print('\nAccessing nested list by negative Index:')

nested_list = list(([ "up",30,2.5,4.5],[12,34,"at"])) 
print(nested_list[0][-1])
print(nested_list[0][-2])
print(nested_list[0][-3])
print(nested_list[0][-4])

print(nested_list[1][-1])
print(nested_list[1][-2])
print(nested_list[1][-3])

Output

Accessing single list element by negative Index:
The
up
into
on
at

Access the nth element: The

Accessing nested list by negative Index:
4.5
2.5
30
up
at
34
12

3 How to Access element of List Python by Range of Indexes


We access the range of elements by index we pass the start and end index in a square bracket([]). It returns a new list of elements those fall in the index range.

pre_List = ['at','on','into','up','The']


print('Accessing  list element range of indexes:\n')

print(pre_List[1:4])

print(pre_List[0:3])

print(pre_List[0:5])

Output

Accessing  list element range of indexes:

['on', 'into', 'up']

['at', 'on', 'into']

['at', 'on', 'into', 'up', 'The']

4. How to Access element of List Python by Range of negative Indexes


In this example, we are accessing multiple elements from the python list by passing a range of negative indexes

  • pre_List[-4:-1] : It will access the element from the index position -4 to -1 that are [‘on’, ‘into’, ‘up’]
  • pre_List[-3:-2] : It will access the element from the index position -3 to -2 that are [ ‘into’]
  • pre_List[-3:-1] : It will access the element from the index position -3 to -2 that are [‘into’, ‘up’]
pre_List = ['at','on','into','up','The']


print('Accessing  list element range of negative indexes:\n')

print(pre_List[-4:-1])

print(pre_List[-3:-2])

print(pre_List[-3:-1])

Output

Accessing  list element range of negative indexes:

['on', 'into', 'up']

['into']

['into', 'up']

Summary

In this post, we have learned How to Access element of List Python with example by using different ways of index that including

  • List Posting Indexing
  • Negative Indexing
  • Range of index