Home > Backend Development > Python Tutorial > Common methods of python list

Common methods of python list

清浅
Release: 2019-03-16 16:34:19
Original
4934 people have browsed it

Commonly used methods in Python list include creating a list, adding new elements, viewing values ​​in the list, deleting elements in the list, sorting and reversing, and list slicing.

Lists are the most commonly used Python Data type, which can appear as a comma-separated value within square brackets. In the following article, we will introduce the common methods of lists in Python in detail. It has a certain reference effect and I hope it will be helpful to everyone.

Common methods of python list

[Recommended course: Python Tutorial]

Common methods of Python list

(1) Create a list

Use commas to separate different data before use Just enclose it in square brackets. The subscript starts from 0. The subscript of the last element can be written as -1

list  =  ['1',‘2,‘3’]
list = [] //空列表
Copy after login

(2) Add a new element

There are three ways to add new elements, namely:

append method: add an element at the end of the list

list.append()
Copy after login

insert method: means to add an element at the specified position, if not specified Add

list.insert(n,'4')
Copy after login

extend method at the end of the list: merge elements in list 1 and list 2

list1.extend(list2)
Copy after login

(3) View the values ​​in the list

You can use the print method to traverse the list. This method is equivalent to for i in list

print(list[n]): means using the subscript index to access the value in the list

print(list.count(xx)): Indicates checking the number of an element in this list. If the element does not exist, it returns 0

print(list.index(xx)): Indicates Find the subscript of this element. If there are multiple ones, return the first one. If you find an element that does not exist, an error will be reported

(4) Delete the element in the list

list.pop(): Delete the last element

list.pop(n): Specify the subscript and delete the specified element. If you delete a non-existent element, an error will be reported

list .remove(xx): Delete an element in the list. If there are multiple identical elements, delete the first one

del list[n]: Delete the element corresponding to the specified subscript

del list: Delete the entire list. After the list is deleted, it cannot be accessed

(5) Sorting and reversing

list.reverse(): Indicates reversing the list

list.sort(): Indicates sorting, ascending order by default

list.sort(reverse=True): Indicates descending order

Note: There are strings and numbers in the list cannot be sorted, the sorting is for the same type

(6) list slicing

Slicing is a method of list value

name[n: m]: Indicates that the slice does not contain the value of the subsequent element

name[:m]: Indicates that if the previous value of the slice is default,

name[n:] will be taken from the beginning. : Indicates that if the value after the slice is defaulted, it will be taken to the end

name[:] : It means that if all values ​​are defaulted, all

name[n:m:s] will be taken: Indicates how many elements to take once, where s represents the step size

If the step length is a positive number, it is taken from left to right

If the step length is a negative number, it is taken from right to left

Note: Slicing also applies to strings, and strings also have subscripts

Summary: The above is the entire content of this article, I hope it will be helpful to everyone.

The above is the detailed content of Common methods of python list. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template