Home  >  Article  >  Backend Development  >  Python list list

Python list list

高洛峰
高洛峰Original
2017-02-28 10:24:511612browse

Operation

1. Add

append(element value): Add an element at the end of the list

insert(n, element value): Add it at the specified index Element

2. Delete

pop(n): When the value n is given, delete the element at index n. If not, delete the last element

del list [n]: Directly delete the given element

list.remove(obj)
Remove the first matching item of a certain value in the list

3. Modify, query

You can access and modify a single element directly through the subscript

a[-1] represents the penultimate element of the list, which is the last element of the list

a = [1, 2, 3, 4, 5]

a[:], a[::] is equivalent to a[0:n:1]: indicating that the index is accessed from 0 to 1 by automatically adding 1 For n-1 elements, the number of elements is n. In fact, it is to access the entire list itself.

This has many functions. For example,

to only get the odd elements of the list, use a[::2]

The odd-numbered elements starting from the third element a[2::2]

Get the reverse order of the even-numbered elements a[1::2]

is a[::-1], equivalent to a[-1::-1], which means accessing in reverse order from the last element of the list

Explanation:

1) The difference between [:] and =

Both of them can achieve assignment, but = is a reference assignment, and the resulting list still points to the original content. If one list is modified, the other will also change

[:] is value assignment, modifying the new or original list has no effect on the other list

4. Others

list.count (obj)
Count the number of times a certain element appears in the list

list.index(obj)
Find the index position of the first matching item of a certain value from the list

list.reverse()
Reverse elements in the list

+: Directly combine two lists into one list


More Python list list For related articles, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Python connects to mysqlNext article:Python connects to mysql