Access element in python list | Laptrinhcanban.com

HOME › >>

Access element in python list

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" ] 
province [1]
>> Hanoi

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" ] 
print (province [ 1 ])
# >> Hanoi

print (province [ 2 ])
# >> Saigon

print (province [ 3 ])
# >> Danang

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" ] 
print (province [- 1 ])
# >> Danang

print (province [- 2 ])
# >> Sài Gòn

print (province [- 3 ])
# >> Hanoi

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" ] 
print (province [ 3 ])

#>>Traceback (most recent call last):
#>> File "", line 2, in <module>
#>> print(province[3])
#>>IndexError: list index out of range

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" ] 
length = len (province)
print (length)

#>> 3

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" ] 
last_inter = province [ len (province) - 1 ]
print (last_inter)

# >> 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" ] 
print (province [ 0 ])
print (province [ 1 ])
print (province [ 2 ])

length = len (province)
print ( 'Number of elements =' , length)

last_ele = province [ len (province) - 1 ]
print ( 'last element =' , last_ele)

URL Link

https://laptrinhcanban.com/en/python/nhap-mon-lap-trinh-python/list-trong-python/truy-cap-phan-tu-trong-list-python/

Thank you for reading and please Like & Share for other friends to learn.
">

HOME  › >>

  • Recent Posts
Profile
きよしです!笑

Author: Kiyoshi (Chis Thanh)

Kiyoshi was a former international student in Japan. After graduating from Toyama University in 2017, Kiyoshi is currently working at BrSE in Tokyo, Japan. Kiyoshi là một cựu du học sinh tại Nhật Bản. Sau khi tốt nghiệp đại học Toyama năm 2017, Kiyoshi hiện đang làm BrSE tại Tokyo, Nhật Bản.