Python:list usage

Source: Internet
Author: User

A list is an ordered set of elements that can be added and removed at any time.

Define an empty list
>>> a_list=[]>>> a_list[]
Ordinary
>>> a_list=[1,2,3,4,5]>>> a_list[1, 2, 3, 4, 5]
Traverse
 for inch a_list:      ... Print  12345
Add Append: Adds an element at the end, adding only one at a time
>>> a_list.append ('Adele')>>> a_list[' Adele ']
Insert: Inserts at any location
>>> A_list.insert (1,'Taylor')>>> a_list[ ' Taylor ' ' Adele ']
Extend: The end is incremented, and the entire value of the other list
>>> a_list.extend (['1989','hello'])> >> a_list['Taylor'Adele'  1989'Hello']
Delete pop: Delete the last/specified position element, only one at a time
>>> A_list.pop ()    # default Delete last value 'hello'  
>>> A_list.pop (1) # Specify delete location 'Taylor'
Remove: Removes the first occurrence of a value in the list
>>> a_list[' 1989'Adele'> >> a_list.remove (1)>>> a_list['1989'  Adele']
Del: Delete one or several elements in succession
>>>delA_LIST[0]#Delete the specified element>>>a_list[2, 3, 4, 5,'1989','Adele']
>>>delA_list[0:2]#remove several elements in succession>>>a_list[4, 5,'1989','Adele']
>>>delA_list#Delete entire list>>>A_listtraceback (most recent): File"<stdin>", Line 1,inch<module>Nameerror:name'a_list' is notDefined
Sort and reverse order
>>> a_list.sort ()>>> a_list['1989 ' Adele']
Reverse order
>>> a_list[' 1989'Adele'> >> a_list.reverse ()>>> a_list['Adele'  1989', 5, 4, 3, 2, 1]
Several operators
>>> [1,2,3]+['a','b','C'][1, 2, 3,'a','b','C']
>>> ['Hello']*4['Hello','Hello','Hello','Hello']
>>> 1inch[A]true

Python:list usage

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.