A convenient list object in Python

Source: Internet
Author: User

Recently, I became interested in Python. So I downloaded some basic Python books and documents from the Internet and started learning python. I found that the list object function in python is very powerful, programming is much easier than other programming languages to perform list operations.

To define a list in Python, you only need the following statement:

Li = ["A", "B", "C", "D"]

List has many functions for list operations, such as extend, append, insert, remove, Pop.

If you want to add a project to the list, you can either add a project separately or add the entire list Li. append ("F") to add a Project A, B, C, D, F

Li. Extend (["F", "G"]) add another list to the end of the list a, B, c, d, f, g

You can also add a project to a specified location.

Li. insert (2, "F") Add "F" to the second location A, B, F, c, d, e
You can also use functions to delete elements.

Remove: deletes an element.

>>> Li
['A', 'B', 'C']
>>> Li. Remove ("")
>>> Li
['B', 'C']

You can directly pass the element to be deleted as a parameter to the function. This element will be deleted from the list.

Pop is to delete the last element in the list and return its value.

>>> Li. Pop ()
'C'

The list in python is the same as the array in C language. The elements in the list are counted from scratch. in Python, the same method can be used to obtain array elements in C language.

To obtain the elements in the list.

Li [0] ""

If the specified location exceeds the index range, Python reports an error.

Li [10]

Python reports an error.

Traceback (most recent call last ):
File "<pyshell #2>", line 1, in <module>
Li [10]
Indexerror: List Index out of range

In python, the element position can be either positive or negative. However, when the element position is negative, the table calculates the element position from the end of the list.

Li a, B, c, d, e

For example, Li [-3] = Li [5-3] = Li [2] = C

However, when obtaining the last element in the list, it should be written as Li [-1].

The most convenient part of list in python is its biggest highlight. in C language, if you want to extract an array and assign it to a new array, the programmer must perform loop operations, in python, you only need one command to solve the problem.

Newli = Li [0: 3]

The value of newli is ["A", "B", "C"]

You only need to specify the start position and end position in brackets to intercept the list. However, note that the returned sub-list includes the elements at the start but not the elements at the end, that is to say, the specified space is pre-closed and post-open, and does not contain elements at the specified end position. This can be confusing during programming and requires special attention.

When the actual location is empty, Table 0, and end position is the last element in the empty age table list

In python, the most convenient function is to add a list. In python, the list is overloaded with operators such as ++, ++ =, and ×.

>>> Li1 = [1, 2, "C"]
>>> Li2 = [3, 4, "a"]
>>> Li1 + = li2
>>> Li1
[1, 2, 'C', 3, 4, 'a']

+ Unlike the list extend method, the extend method adds elements in the new list based on the original list, however, the + operator combines the original list with the added list to generate a new list. Therefore, the + = function is equivalent to the extend function.

This article only explains the List objects in Python. As you learn more about Python, other blog posts will be shared with you.

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.