Python basic data Type-list

Source: Internet
Author: User

1. Create a list

>>> list1 = [' Anmi ', ' kan ', ' seven ']


2. View the list

>>> list1[' Anmi ', ' kan ', ' seven ']>>> list1[0] ' Anmi ' >>> list1[1] ' kan ' >>> list1[2] ' Seven '


3. Append elements

>>> list1.append (' Selina ') >>> list1.append (' kan ') #元素值可以重复 >>> List1


4. Index

>>> list1[' Anmi ', ' kan ', ' Seven ', ' Selina ', ' kan ']>>> list1.index (' kan ') 1 #从左开始查找到第一个匹配的后, on output


5. Statistics

>>> list1.count (' kan ') 2>>>


6. Insert

>>> List1.insert (2, ' Eric ') >>> list1[' Anmi ', ' kan ', ' Eric ', ' Seven ', ' Selina ', ' Kan ']


7. Delete the last element

>>> list1.pop () ' Kan '


8. Delete the specified element

>>> list1[' Anmi ', ' kan ', ' Eric ', ' Seven ', ' Selina ']>>> list1.remove (' Eric ') >>> list1[' Anmi ', ' kan ', ' Seven ', ' Selina '] Delete all specified elements list1 = [' kan ', ' Seven ', ' Kan ', ' Eric ', ' kan ']for i in range (list1.count (' kan ')): List 1.remove (' kan ') print (List1)


9. Reversal

>>> list1[' Anmi ', ' kan ', ' Seven ', ' Selina ']>>> list1.reverse () >>> list1[' Selina ', ' seven ', ' Kan ', ' anmi ']


10. Sorting

>>> list1.sort () >>> list1[' Anmi ', ' kan ', ' Selina ', ' seven ']



This article is from the "guoxianqi2016" blog, make sure to keep this source http://guoxianqi2016.blog.51cto.com/11407620/1762196

Python basic data Type-list

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.