Python built-in lists (list)

Source: Internet
Author: User

A list of data types is built into Python.

The list list is a built-in, ordered collection. You can add and remove data from the list at any time.

list is represented by [].

1 >>> figure = ['1','2','3  ']2print(figure)3 ['1  '2'3']

The figure in the figure is a list.

    

You can query the number of elements in the list by using the Len () function.

1 >>> len (figure)2 3

Len ()-1 You can query the position of the last element in the list.

>>> a = ['a','b','c' ,'d']>>> len (a) -13>>> Len (a)-22

        

You can use an index to query a specific element. The index starts at 0.

>>> figure[0]'1'>>> figure[1]'2 '>>> figure[2]'3'>>> figure[3]traceback ( Most recent call last):  '<stdin>' in <module> Indexerror:list Index out of range

An error occurs if the element index is outside the range of the element being looked up.

You can also query backwards, the last one is-1, and so on, the second to the bottom is-2 and so on.

>>> figure[-1]'3'>>> figure[-2]'2  '>>> figure[-3]'1'>>> figure[-4] Traceback (most recent):  '<stdin>' in < module>indexerror:list Index out of range

List is a mutable ordered list, so you can implement appending elements at the end of the list: the Append method.

>>> figure.append ('4')>>> figure['1  '2'3'4 ']

You can also add elements at a specified location using the function implementation: Insert method.

>>> figure.insert (0,'0')>>> figure['  0"1"2"3 ' ' 4 ']

You can also implement the delete element of list.

>>> figure.pop ()'4'>>> figure['0  '1'2'3 ']

    

The last element is deleted by default when none is specified in ().

>>> figure.pop ()'4'>>> figure['0  '1'2'3 ']

Deletes the specified element when the pop () specifies the position of the element

>>> figure.pop (0)'0'>>> figure['1  '2'3']

You can also reassign a value directly to the element at the specified location.

' 2 '>>> figure['2'2' 3 ']

The list can be placed in different types of elements at the same time.

>>> L = ['A', 123, True]>>> l[' A ', 123, True]

Lists can also be nested with each other.

>>> s = ['a','b','C',['1','2','3'],'D']>>>s['a','b','C', ['1','2','3'],'D']>>>Len (s)5>>> Len (s) -14>>> s[-1]'D'>>> s[-2]['1','2','3']
>>> r =[True]>>>R[true]>>> S.insert (3, R)>>>s['a','b','C', [True], ['1','2','3'],'D']

These two lists are nested called two-dimensional arrays, positioning the two-dimensional need [] positioning, like the vertical and horizontal coordinates, three-dimensional needs [][][], and so on.

 >>> s[  " a  " ,  '  b  " ,  '  c  " , [True], [ " 1   ", "  2   ", "  3   "], "  d   " ]  > >> print  (S[4][0])  1 

It is also possible to create an empty list.

>>> a = []>>> a[]>>> Len (a) 0

Python built-in lists (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.