python--List

Source: Internet
Author: User

Creation of lists

1, common method to create a list

list = []


2, list analysis, List deduction type

Using list parsing can be almost 1 time times faster than using a normal method. It is therefore recommended to use list parsing.

Grammar

[Expression for Iter_val in iterable if COND_EXPR]
Requirements: List 1~10 of all numbers squared >>>l = [i**2 for i in range (1,11)]>>>print l[1, 4, 9, 16, 25, 36, 49, 64, 8 1, +]  

Updates to the list

 >>> list = [1,2,3 >>> list.append ("  4   ") #   add list item  >>> list[ 1, 2, 3,  " 4  "    >>> del  list[3] #   delete list item  >>> list[ 1, 2, 3] 
>>> List1 = [All in all]>>> list2 = [4,5,6]>>> list1.extend (list2)# append multiple values at once >>> list1[1, 2, 3, 4, 5, 6]
>>> list = [1,2,2,3,4]>>> list.remove (2)# Remove the first occurrence of a value in the list >>> list[1, 2, 3, 4]>>> list.pop ()# removes an element from the list (the last element by default) and returns the value of the element 4 >>> list[1, 2, 3]
>>> list = [List.insert]>>> 1,4# insert >>> list[1, 4, 2, 3]

List Common methods

List.index ()

# Find the element location >>> list = [List.index]>>> A (2)1

List.count ()

List.reserve ()

# Invert >>> list = [List.reverse]>>> ()>>> list[ 3, 2, 1]

List.clear ()

# Empty list >>> lists = [list.clear]>>> ()>>> list[]

List.sort ()

>>> list = [1,3,2,6,5]>>> list.sort ()# sorting >>> list[1 , 2, 3, 5, 6]

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