How to Read Element in Python List
We know how to create the list and update the list by various built-in methods of the list. In the Python Programming Language, a list can be accessed either by index or slice operator. In this tutorial, we will learn how to read elements in the list using the index
method, slice
operator, and for loop.
The list follows the zero-based index
. Index starts from 0. In the list, the index of the first element is 0, and the index of the second element is 1, and so on. The list supports both +ve and -ve indexes. Another way of accessing elements from the list is by the slice
operator. We can access elements both by positive index and negative index.
Example: Reading elements of a list by positive index
The positive index starts from 0. It prints elements forward from the start of the list.
The below example shows how to access elements from the list through the positive index.
#Intializing list
list=["python",10,0.7895,True,50.2145,100]
print("Elements present in list are:",list)
#Reading elements of list by index method.
print(list[0])
print(list[1])
print(list[2])
print(list[3])
print(list[4])
print(list[5])
In the above example, we are reading individual elements present in the list from index 0 to 5.
Once we run the program, it shows the following output.
Elements present in list are: ['python', 10, 0.7895, True, 50.2145, 100]
python
10
0.7895
True
50.2145
100
Example: Reading elements of a list by negative index.
The negative index starts from -1. It prints elements backward from the end of the list. The below example shows how to access elements from the list through the positive index.
#Intializing list
list=["python",10,0.7895,True,50.2145,100]
print("Elements present in list are:",list)
#Reading elements of list by negative index method.
print(list[-1])
print(list[-2])
print(list[-3])
print(list[-4])
print(list[-5])
print(list[-6])
In the above example, we are reading individual elements present in the list from index 0 to 5.
Once we run the program, it shows the following output.
Elements present in list are: ['python', 10, 0.7895, True, 50.2145, 100]
100
50.2145
True
0.7895
10
python
Example: Reading Elements of a list using Slicing
We can access elements from the list through the positive index and negative index also.
Unlike in the first method, if we give index value out of range, we will not get any error. Instead, it takes default values.
The below example shows how to access elements using the slice operator.
#Intializing list
list=["python",10,0.7895,True,50.2145,100]
print("Elements present in list are:",list)
print(list[1:3:1])
print(list[-4:-1])
print(list[-1:-4:-1])
print(list[3:100])
In the example first, we defined a list of elements.
In code line 4, we gave start=1, end=3, step=1. It will print elements from index 1 to 2 (as the end value will not include) with increment 1.
In code line 5, we gave start=-4, end=-1 and we did not give step value but it will take the default value i.e 1. It will print elements from the index -3(as the end value will not include) to -1 with increment 1.
In code line 6, we gave start=-1, end=-4, and step=-1.It will print elements of the list from the backward direction from the index value -1 to -3.
In code line 7, we gave index value out of range. We will not get an error, but it will print elements from the specified start index to the end list.
Elements present in list are: ['python', 10, 0.7895, True, 50.2145, 100]
[10, 0.7895]
[0.7895, True, 50.2145]
[100, 50.2145, True]
[True, 50.2145, 100]
Example: Reading elements of a list using the for loop
Using for loop we can access elements of the list.
#Intializing list
list=["python",10,0.7895,True,50.2145,100]
print("Elements present in list are:",list)
for i in list:
print(i)
Elements present in list are: ['python', 10, 0.7895, True, 50.2145, 100]
python
10
0.7895
True
50.2145
100
Conclusion:
In this tutorial, we learned to access elements of the list by index, using slice operator and using the for a loop. We solved examples of these functions and access elements from the list.