The various functions of the Python list use the

Source: Internet
Author: User
Tags python list

First, let's start by understanding what a list is: A sequence is the most basic data structure in Python, similar to an array. Each element in the sequence is assigned a number-its position, or index (index), also called subscript the first index is 0, the second index is 1, and so on.

Create a list:

names=['xiaoming','libai','Zhangfei ','wangba']

Add at the end:

Names.append ('+')
print (names) ['xiaoming'libai ' Zhangfei ' ' Wangba ' '  - ']

Specify location insertion:

Names.insert (2,'xiaohei')
print (names) ['xiaoming'libai'Xiaohei  'zhangfei' 'wangba'  []

Specify location deletion:

Names.remove ('libai') #方法1del  names[0] #方法2
print (names) ['Pig''zhangfei'Wangba  []
#names. Pop ()  #默认删除最后一个, or you can delete a specific location if you add an index

Get the index of a string:

Print (Names.index ('wangba'))2


print (names) ['xiaoming'libai'Pig'  'zhangfei'wangba'250  ']   

Count the number of a string:

names=['xiaoming','libai','Zhangfei ','wangba','libai','  Libai']print(Names.count ('libai') )3

Reverse list order:

Names.reverse () Print (names) ['libai'libai'Wangba  'zhangfei' 'libai'  xiaoming']

Sort by ASCII code:

Names.sort () Print (names) ['libai'libai'Libai  'wangba' 'xiaoming'  Zhangfei'

List Merge:

names=['xiaoming','Libai','Zhangfei','Wangba','Libai','Libai']names1=['1','2','3']names.extend (names1) ['Libai','Libai','Libai','Wangba','xiaoming','Zhangfei','1','2','3']#names1 won't go away .

Clear list:

names.clear () Print (names) []

The various functions of the Python list use the

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: 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.