Access Elements From A List in Python
Now, we are quite familiar with a few things in relation with the list, like how we can create some list, and how we can get the length of the list. So now, further exploring the list, we are going to have a look at how we can access the elements from the list.
Basically, this is important for us, since we are storing multiple elements into the list, and we have one reference variable. So there can be a question that how we can access those elements from the list. There is a simple answer to this question, which we are going to explore now.
Well, first of all, if you want to print the complete list, you can simply use the print function, and within that, you can directly give the list reference variable. Have a look at the below program, which tries to demonstrate how we can print the whole list.
Access Elements From A List in Python
As you can see in the above program, we are directly printing the list. So, whenever you want to print the list in python, you can simply have it directly.
Now, Let’s talk about what we can do, if we need to access the individual elements from the list. In such situations, we have something called as index. The indexes for the list in python, start from zero, which means that the first element in the list has the index 0, the second element has index 1, and so on. The last element in the list has the index of length -1. Let’s have a look at a simple program, which demonstrates about accessing the individual elements from the list.
As you can see, in the above program, we are trying to access the individual elements from the list. To achieve this, we are using the index. To access the individual elements here, we are simply writing the reference variable, followed by the square brackets, and within the square brackets, we are providing the index of the element that we want to access.
First of all, we are trying the 0 index element, which is the first element from the list. Then we are trying to access the index 4 element, which is True from the list, and then we are trying to access the last element from the list, which has the index of length – 1. Let’s have a look at the output of the above program.
1
True
GyaniPandit
As you can see, we are able to access the individual elements from the list, with the help of index. The thing is that, if we try to give such an index, which is out of the range for the list, like for example our list has 6 elements in the previous example, so we have elements up to index 5(since indexing starts from zero). But if we try to access element with index 20 for example, which is out of range for the list, then we are going to get into error. Let’s have a look at this now –
As you can see, in the above program, we are first trying to access some valid indexes, and then in the last print statement, we are trying to access an invalid index, which results into error. You can try to see the output of the above program, by executing it, but the error that we get here, is something like this –
IndexError: list index out of range
As you can see, accessing the invalid indexes, we went into error. So, whenever we need to access the individual elements from the list, we can make use of the indexes. Earlier, we had mentioned that the list is a mutable sequence of elements, which simply means that we can change an element/item in the list, by simply accessing it directly. So now, Let’s explore how we can change the elements in the list by accessing them.
As you can see, in the above program, we have a list, and here, we are trying to change the zero index element from the list, and we are just doing that with simple assignment. Also, in the next line, we are trying to print the list, and in the output, we can simply find that the element at the 0th index has been changed. Let’s have a look at the output of the above program –
[‘Python’, 2, 3, 4, True, ‘GyaniPandit’]
As you can see, the element at the 0th index in the list was earlier 1, and now it has been changed to the string ‘Python’, and we could do it just by simple assignment at that index. Again, here as well, if we try to access some invalid index, we get into error.
You can try changing some more elements from the list, just by doing such simple assignment, and observe the outputs.
Since we are exploring about accessing the individual elements from the list, and we are dealing with the indexes in the list, let’s talk about the negative indexing as well. The thing is that in python, negative indexing is also allowed for the sequences. Here, in the negative indexing, the index of -1 refers to the last element from the list, the index of -2 refers to the last second element, and so on. So, Let’s have a look at a simple program, which demonstrates about the negative indexing to access the individual elements in the list.
As you can see, we are able to access the individual elements with the negative indexing. So, whenever we need to access the last element from the list Let’s say, and we do not want to calculate the index of that element, we can simply make use of the negative indexing. Let’s have alook at the output –
GyaniPandit
True
As you can see, we could access the individual elements with the help of negative indexing. So, at times, as and when required, you can make use of the negative indexing, to access the elements.