# Beginning Python from Novice to Professional (3)-list operation

Source: Internet
Author: User

List operations

List function:

`>>> list (' Hello ') [' H ', ' e ', ' l ', ' l ', ' O ']`
Change the list:

`>>> x=[1,1,1]>>> x[1]=2>>> x[1, 2, 1]`
To delete an element:

`>>> names = [' Wu ', ' Li ', ' Zhao ', ' Qian ']>>> del names[1]>>> names[' Wu ', ' Zhao ', ' Qian ']`
Shard Assignment:

`>>> name = List (' perl ') >>> name[' P ', ' e ', ' r ', ' L ']>>> name[2:] = list (' ar ') >>> name[ ' P ', ' e ', ' a ', ' R ']`
`>>> num = [1,2,3,4,5]>>> num[1:4]=[] # starts with 1 bits but does not include 4 bits >>> num[1, 5]`
Add a new element at the end of the Append list:

`>>> lst = [1,2,3]>>> lst.append (4) >>> lst[1, 2, 3, 4]`
Number of Count statistics elements:

`>>> [' we ', ' have ', ' we ', ' a ', ' Dog '].count (' we ') 2`
Extend extension list:

`>>> a = [1,2,3]>>> B = [4,5,6]>>> A.extend (b) >>> a[1, 2, 3, 4, 5, 6]`
Index to find the location of the first occurrence:

`>>> sentence = [' I ', ' have ', ' a ', ' little ', ' dog ']>>> sentence.index (' Little ') 3`
Insert inserts an object into the list:

`>>> num = [1,2,3,5,6,7]>>> num.insert (3, ' four ') >>> num[1, 2, 3, ' Four ', 5, 6, 7]`
Pop removes the list element, the last one by default:

`>>> x = [1,2,3]>>> x.pop () 3>>> x[1, 2]>>> x.pop (0) 1>>> x[2]`
Combined Append:

`>>> x = [1,2,3]>>> x.append (X.pop ()) >>> x[1, 2, 3]`
Remove removes the first occurrence from the list:

`>>> x = [' To ', ' is ', ' or ', ' not ', ' to ', ' is ']>>> x.remove (' is ') >>> x[' to ', ' or ', ' not ', ' to ', ' Be ']`
Reverse Reverse storage elements:

`>>> x = [1,2,3]>>> x.reverse () >>> x[3, 2, 1]`
Sort order:

`>>> x = [4,6,2,1,7,9]>>> x.sort () >>> x[1, 2, 4, 6, 7, 9]`
`>>> X.sort (reverse=true) >>> x[9, 7, 6, 4, 2, 1]>>> x.sort (reverse=false) >>> x[1, 2, 4 , 6, 7, 9]`

Beginning Python from Novice to Professional (3)-list operation

Related Keywords:

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.