Posted at: https://laptrinhcanban.com/en
Instructions on how to access element in python list. You will learn how to access elements in Python lists using indexes, positive and negative index in python, how to access last element in python listafter this lesson.
We can access the element in the Python list through the index of that element in the list.
Here’s how to do it:
Access elements in Python list using index
We access the elements in the Python list by index with the following syntax:
list
[index
]
Inside:
- list is the list for which we need to access Element
- index is the index of the element in the list that we need to access.
For example
province = [ "Thanh Hoa" , "Hanoi" , "Saigon" , "Da Nang" ] |
Positive and negative index in python
Positive indexing in python/h3>
Each element in the list is assigned an index - the ordinal number that specifies the position of that element in the list.
The first element of the list has an index 0
, then ascending 1, 2, 3…in reverse order.
When specifying an element’s index, each element will have only its own index in the list, so that we can access and retrieve the value of that element in the list by specifying the index of the element. there.
For example, the index element in the list is [“Thanh Hoa”, “Hanoi”, “Saigon”, “Da Nang”] as follows:
["Thanh Hoa", "Hanoi", "Saigon", "Da Nang"] ----------------------------------------- 0 1 2 4
Let’s look at an example, with this indexing, we will access, get the value of each element and print to the screen in the following order:
province = [ "Thanh Hoa" , "Hanoi" , "Saigon" , "Da Nang" ] |
Negative index in python list
In addition to specifying an index with positive numbers, we can do the opposite by specifying an index with negative numbers.
The last element (right-hand side) of the list has an index of -1
, then descends -1, -2,-3…towards the top (left-hand side).
For example, the index element in the list is [“Thanh Hoa”, “Hanoi”, “Saigon”, “Da Nang”] as follows:
["Thanh Hoa", "Hanoi", "Saigon", "Da Nang"] --------------------------------------------- -4 -3 -2 -1
Let’s rewrite the above example by specifying the index as a negative number as follows:
province=[ "Thanh Hoa" , "Hanoi" , "Saigon" , "Da Nang" ] |
IndexError
Note that when accessing elements in Python list via index, if you specify an index that does not exist in the list, the IndexError
will occur .
province = [ "Hanoi" , "Saigon" , "Da Nang" ] |
This error occurs, like you are trying to get money from your wallet althought there was no money left in your wallet.
Access last element in python list
We can access last element in python list by specifying that element’s index through the len() function .
Len() in python is a function used to find the length or number of elements of an object in python. If this object is list, the function will output the number of elements in the specified Python list
You can find out details about the len () function in the article Len() trong Python.
We will count the number of elements in Python list in the above example as follows:
province = [ "Hanoi" , "Saigon" , "Da Nang" ] |
Since the index of the first element in the list is equal 0, its index phần tử cuối cùngwill be equal số phần tử -1.
So when we access the last element in the python list , we write with the following syntax:
list
( len
(list
) -1)
Let’s try to access the last element of the list in the above example as follows:
province = [ "Hanoi" , "Saigon" , "Da Nang" ] |
Summarize and practice
Above, Kiyoshi showed you how to access elements in Python list . To better understand the lesson content, you should practice with the following examples.
And let’s learn more about Python in the next lessons.
province = [ "Hanoi" , "Saigon" , "Da Nang" ] |
URL Link
HOME › python for beginners>>list in python