Python List &tuple

Source: Internet
Author: User

1        Listand theTuple1.1   List(ordered repeatable modifiable)

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

>>> list=[1,2,3]

>>> List

[1, 2, 3]

>>> list=[' 1 ', ' 2 ', ' 3 ']

>>> List

[' 1 ', ' 2 ', ' 3 ']

>>> len (list)-- displays The number of elements in the list

3

>>>List[0] --the number of element indexes starts from 0 , that is, from left to right the first is 0 , and Redis Similar in

' 1 '

>>> List[1]

' 2 '

>>> List[2]

' 3 '

>>>List[-1] --The penultimate, the rightmost element , similar to Redis

' 3 '

>>> List[-2]

' 2 '

The list is a mutable, ordered table, so you can append an element to the end of the list :

>>> list.append (' 4 ')-- Note the brackets, not []

>>> List

[' 1 ', ' 2 ', ' 3 ', ' 4 ']

>>> List.append (4)

>>> List

[' 1 ', ' 2 ', ' 3 ', ' 4 ', 4]

inserts an element into the specified position , such as where the index number is 1

>>> List.insert (1, ' x ')--1 represents the index position,x represents the element

>>> List

[' 1 ', ' X ', ' 2 ', ' 3 ', ' 4 ', 4]

Delete Element , the last bit is deleted by default, and you can specify an element at a location

>>> List

[' 1 ', ' X ', ' 2 ', ' 3 ', ' 4 ', 4]

>>> List.pop ()-- Delete the last element by default

4-- returns the deleted element

>>> List

[' 1 ', ' X ', ' 2 ', ' 3 ', ' 4 ']

>>> List

[' 1 ', ' X ', ' 2 ', ' 3 ', ' 4 ']

>>> List.pop (1)-- Specify the deletion of the 1-position element

' X '

>>> List

[' 1 ', ' 2 ', ' 3 ', ' 4 ']

To replace an element with another element , you can assign a value directly to the corresponding index position

>>> List

[' 1 ', ' 2 ', ' 3 ', ' 4 ']

>>> list[0]= ' 0 '

>>> List

[' 0 ', ' 2 ', ' 3 ', ' 4 ']

List The data type of the elements inside can also be different

>>> List

[' 1 ', ' X ', ' 2 ', ' 3 ', ' 4 ', 4]

List in list

>>> List

[' 1 ', ' 2 ', ' 3 ', ' 4 ']

>>> List.insert (1,[' 1 ', ' 2 '])

>>> List

[' 1 ', [' 1 ', ' 2 '], ' 2 ', ' 3 ', ' 4 ']

>>> List[1][1]-- Some like two-dimensional arrays

' 2 '

Empty List

>>> l=[]

>>> Len (L)

0

1.2   Tuple(orderly, repeatable, non-modifiable)

It is also an ordered list,and the tuple and list are very similar, but the tuple cannot be modified once initialized.

>>> (t=)

>>> T

(1, 2)

>>> t= ()-- empty tuple

>>> Len (t)

0

when there is only one element in a tuple, the definition takes a comma

>>> t= (1,)

>>> T

(1,)

>>> T[0]

1

Create a "variable" tuple

>>> t= (1,2,[1,2])

>>> t[2][0]= ' x '

>>> t[2][1]= ' y '

>>> T

(1, 2, [' X ', ' Y '])


This article is from the "90SirDB" blog, be sure to keep this source http://90sirdb.blog.51cto.com/8713279/1795431

Python List &tuple

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.