Python Basics: List

Source: Internet
Author: User

About 0x00 

The Python list object is the most common sequence that the language provides. A list is an ordered collection of positions related to an arbitrary type of object , and it has no fixed size.

The features of the list can be summarized as four points: 1, ordered set, 2, indexed by offset, 3, nested, 4, type variable.

0X01 Basic Operations

' Captain ' # A List of three Diffirent-type objects # Number of items in the list3

We are able to index, slice, and manipulate the list

>>> L[0]#indexing by Position123>>> L[:-1]#slicing a list returns a new list[123,'Captain']>>> L + ['Heisenberg', 456,'Linux']#Concatention makes a new list too[123,'Captain', 3.14,'Heisenberg', 456,'Linux']>>> L#We ' re not changing the original list[123,'Captain', 3.14] 

0x03 Add action

Generate a new list

>>> a = [1, 2, 3]>>> b = [4, 5, 6]>>> A +b[1, 2, 3, 4, 5, 6]   #generate a new list

Extend method: Receives the parameter and adds each element of the parameter to the original list

>>> a = [1, 2, 3]
>>> B = [4, 5, 6]
>>> a.extend (b)>>> a>>> [1,2,3,4,5,6]

Append method: Add any object to the end of the list

>>> a = [1, 2, 3]
>>> A.append (4)
>>> A
[1, 2, 3, 4]

Insert method: Inserts any object into the list, you can control the insertion position

>>> a = [1, 2, 3]'a')>>> a[' a ', 2, 3]

0x04 Modification and deletion

>>> a = [1, 2, 3]
>>> a[1] = ' Change '
>>> A
[1, ' Change ', 3]

Del: Deletes the element at the specified location by index

Remove: The first matching value of a specified value in the overflow list

Pop: Returns the last element and removes it from list

>>> a = [1, 2, 3, 4, 5, 5]del  a[0]>>> A [2, 3, 4, 5, 5]> >> A.remove (5)>>> a[2,3,4,5] #remove the first matching item

>>> A.pop ()
5
>>> A
[2,3,4]

0X05 member Relations

In,not in to determine whether an element is within the list, returns a Boolean value

>>> a = [1,2,3,4] in atrue not in atrue

0X06 List Push-down

 for  in range (1,11)[1,2,3,4,5,6,7,8,9,10]>>> range (1,11,2) [ 1,3,5,7,9]forif x% 2 = = 1[1,3,5,7,9]

0x07 Sort Flip

Sort: Arrange the list from small to large

Reverse: Arrange lists from large to small

Both of these methods are directly modifying the original list

>>> a = [33,11,22,44]>>> b = a.sort ()>>> a[11,22,33,44] >>> C = a.reverse ()>>> A [44,33,22,11]

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