A detailed analysis of list () lists in Python

Source: Internet
Author: User
The list is the most flexible, ordered collection object type in Python. Unlike strings, a list can contain objects of any kind: numbers, strings, custom objects, and even other lists, columns

Tables are mutable objects that can be modified in place and implemented by specifying offset values and shards, list method calls, DELETE statements, and so on.

Common methods in the list:

1.append (x): Adds an element to the end of the list.

>>> list=[1,2,3,4,5,6]>>> Print list[1, 2, 3, 4, 5, 6]>>> list.append (7) >>> List.append (8) >>> print list[1, 2, 3, 4, 5, 6, 7, 8]>>>

2.extend (L): Expands the list by adding all the elements of the specified list.

>>> list[1, 2, 3, 4, 5, 6, 7, 8, ten, one, 12]>>> l=[100,200,300,400]>>> list.extend (L) >>&G T Print List[1, 2, 3, 4, 5, 6, 7, 8, ten, one, A, a, a, a, 400]>>>

3.insert (I,X): Inserts an element at the specified position. The first parameter is the index of the element that is ready to be inserted before it.

>>> print List[1, 2, 3, 4, 5, 6, 7, 8, ten, one,,, 400]>>>, List.insert (2,1000) >>> ; Print List[1, 2, 3, 4, 5, 6, 7, 8, ten, one, A, a, a, a, 400]>>>

4.remove (x): Removes the first element in a linked list that has a value of x. If there are no such elements, an error is returned.

>>>> print List[1, 2, 3, 4, 5, 6, 7, 8, ten, one, A, a, a, a, 400]>>> list.remove (+) ;>> list[1, 2, 3, 4, 5, 6, 7, 8, ten, one, A, a, a, a, 400]>>>

5.pop (i): Removes the element from the specified location in the list and returns it. If no index is specified, A.pop () returns the last element. The element is removed from the linked list.

>>>> list[1, 2, 3, 4, 5, 6, 7, 8, ten, one,,, 400]>>>, List.pop (3)


4

>>> list[1, 2, 3, 5, 6, 7, 8, ten, one, one,,, 400]>>> list.pop () 400>>> list[1, 2, 3, 5, 6, 7, 8, ten, one, A, a, 300]>>>

6.index (x): Returns the index of the first element in the linked list that has a value of x.

>>>> list[1, 2, 3, 5, 6, 7, 8, ten, one, A, a, a, 300]>>> list.index (6) 4>>>

7.count (x): Returns the number of occurrences of x in the linked list.

>>>> list[1, 2, 3, 3, 3, 5, 6, 7, 8, ten, one,,, 300]>>> list.count (3) 3>>> list.co UNT (1>>>)

8.sort (): Sorts the elements in the linked list appropriately.

9.reverse (): Reverse the elements in the list.

Use the list as a stack

The linked list method makes it easy for the list to be used as a stack, and the stack acts as a specific data structure, and the first entry element is released (last-in, first-out). Use the Append () method to

To add an element to the top of the stack. A pop () method that does not specify an index can release an element from the top of the stack.

Use the list as a queue

The list can also be used as a queue, the queue as a specific data structure, the first entry of the element is first released (FIFO). Use the Append () method to add elements to the queue at the end of the 0

Calling the Pop () method for a parameter can release the first entry element.

List Delete element: With Del, it can also be deleted as a segment.

>>> list[1, 2, 3, 3, 3, 5, 6, 7, 8, ten, one,,, 300]>>> del list[2:4]>>> list[1, 2, 3 , 5, 6, 7, 8, ten, one, 300]>>> del list[0]>>> list[2, 3, 5, 6, 7, 8, 10, 11, 12, 100, 200, 300]>>> del list[5:]>>> list[2, 3, 5, 6, 7]>>> del list[0:]>>> list[]>>> lis T.append (1) >>> list.append (2) >>> list.append (3) >>> list[1, 2, 3]>>>

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.