Python creates a list and accesses the list by index

Source: Internet
Author: User

Python Create List


one of the data types built into Python is the list: lists. A list is an ordered set of elements that can be added and removed at any time.
For example, by listing the names of all the classmates in the class, you can use a list to indicate:
>>> [' Michael ', ' Bob ', ' Tracy ']
[' Michael ', ' Bob ', ' Tracy ']
A list is an ordered set of mathematical meanings, that is, the elements in the list are arranged in order.

Constructing the list is very simple, using the code above to enclose all elements of the list directly, which is a list object. In general, we assign a list to a variable so that we can refer to the list by a variable:

>>> classmates = [' Michael ', ' Bob ', ' Tracy ']
>>> Classmates # Print the contents of a classmates variable
[' Michael ', ' Bob ', ' Tracy ']
Because Python is a dynamic language, the elements contained in the list do not necessarily require the same data type, and we can include all kinds of data in the list:

>>> L = [' Michael ', +, True]
An element that does not have a list is an empty list:

>>> empty_list = []

Python accesses list by index
Since list is an ordered collection, we can use a list to indicate from high to low the 3 classmates in the class:
>>> L = [' Adam ', ' Lisa ', ' Bart ']
So how do we get the names of the nth-named students from the list?
It is important to note that the index starts at 0, that is, the index of the first element is 0, the second element is indexed by 1, and so on.
Therefore, to print the first name of the classmate, use L[0]:
>>> print l[0]
Adam
To print the name of the first two students, with L[1]:
>>> print l[1]
Lisa
To print the name of the first three students, with L[2]:
>>> print l[2]
Bart
To print the name of the first four students, using l[3]:
>>> print l[3]
Traceback (most recent called last):
  File "<stdin& gt; ", line 1, in <module>
Indexerror:list index out of range
Error! Indexerror means that the index is out of range because the list above has only 3 elements, and the valid index is 0,1,2.
So when using the index, be careful not to cross the border.

Python's Reverse access list
We still use a list to score from high to low to show 3 students in the class:
>>> L = [' Adam ', ' Lisa ', ' Bart ']
At this time, the teacher said, please score the lowest students stand out.
To write code to accomplish this task, we can count the list first and find that it contains 3 elements, so the index of the last element is 2:
>>> Print l[2]
Bart
Is there a simpler way?
Yes! Bart classmate is the last, commonly known as the countdown first, so we can use the 1 index to represent the last element:
>>> Print L[-1]
Bart
Bart classmate says lying on the gun.
Similarly, the second-to-last use-2 means that the reciprocal third use-3 means that the penultimate fourth uses-4 means:
>>> Print L[-2]
Lisa
>>> Print l[-3]
Adam
>>> Print L[-4]
Traceback (most recent):
File "<stdin>", line 1, in <module>
Indexerror:list index out of range
L[-4] Error, because the penultimate fourth does not exist, there are only 3 elements.
When using a reverse index, also be careful not to cross the border.

Python creates a list and accesses the list by index

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.