Python list built-in type method

Source: Internet
Author: User
Tags python list

List action Methods
Action on List L
For example
List = range (1,5) #即 list=[1,2,3,4], with no last element
List = range (1, ten, 2) #即 list=[1, 3, 5, 7, 9]

List.append (obj)
Add a new object at the end of the list
2 List.count (obj)
Count the number of occurrences of an element in a list
3 List.extend (SEQ)
Append multiple values from another sequence at the end of the list (extend the original list with a new list)
4 List.index (obj)
Find the index position of the first occurrence of a value from the list
5 List.insert (index, obj)
Inserting an object into a list
6 List.pop ([Index=-1])
Removes an element from the list (the last element by default), and returns the value of the element
7 List.remove (obj)
To remove the first occurrence of a value in a list
8 List.reverse ()
Reverse List of elements
9 List.sort (Cmp=none, Key=none, Reverse=false)
Sort the original list

Del List[1] #删除指定下标的元素
Del List[1:3] #删除指定下标范围的元素12345
Del List # Deletes list, then list does not exist

List[1:] =list # list = [2,3,4] Slice operator for sub list extraction
LIST[2] # Read the third element in the list
List[-2] #读取列表中倒数第二个元素
List[1:] # Starting from the second element to intercept a list
List1 = list[:] #list1为list的克隆, that is, another copy *

CMP (List1, List2) #比较两个列表的元素
Len (list) #列表元素个数
Max (list) #返回列表元素最大值
Min (list) #返回列表元素最小值
List (seq) #将元组转换为列表

3 in [1, 2, 3] True #元素是否存在于列表中
For x in [1, 2, 3]: print x, #1 2 3 iterations
[2]* 4 #表示为 [2,2,2,2]

Python list built-in type method

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.