7. List

Source: Internet
Author: User

List is ordered, starting from 0 to Length-1

What the list can do: index, slice, add, multiply, check members.

1. Create a list

1 list = ['google'roob', 1997, +]   # can contain different data types

2. Access the values in the list

Use subscript index or square brackets to intercept characters

List1 = ['Google'YouTube', 1990= [ 1,2,3,4,5,6,7,8,9]print(list1[0])  #Googleprint#  [2, 3, 4, 5]

3. Update the list

List1 = ['Google'YouTube', 1990]Print (list1[0])  #Google"char"print(list1[0 ])  #Char

4. Delete List

List1 = ['Google'YouTube', 1990]Print (List1)  #[' Google ', ' YouTube ', 1990,]del  list1[0]  Print(list1)  #[' YouTube ', 1990]

5. List Footstep operator

Print(Len ([+]))#Length: 3Print([1,2,3]+[4,5,6])#combination: [1, 2, 3, 4, 5, 6]Print(["Google"]*4)#repeat: [' Google ', ' Google ', ' Google ', ' Google ']Print(3inch[+/-])#element is present in the list: True forIinch[A]:    Print(i)#iteration of the generations

6. Interception and splicing

L = ["Google","YouTube","Facebook"]Print(L[2])#FacebookPrint(L[-2])#YouTubePrint(l[1:])#[' YouTube ', ' Facebook ']s = [1,2,3,4]Print(L+s)#[' Google ', ' YouTube ', ' Facebook ', 1, 2, 3, 4]

7. Nesting

8. Functions

L = [n/a = (#)#The number of elements in the Len list is print#  3#  List of the largest elements print#3# list min and element print  #1# convert tuples to list print#[1, 2, 3]

9. Methods

L = [n/A]#add an object at the end of the listL.append (8)Print(l)#[1, 2, 3, 8]#Count the number of occurrences of an element in a listPrint(L.count (1))#1#append multiple values of another sequence at the end of a listL.extend ([A])Print(l)#[1, 2, 3, 8, 1, 2, 3]#finds the first occurrence of a specified value in a list, specifying the start and end position of the queryPrint(L.index (2))#1Print(L.index (2,2,6))#5#inserts an element at the specified position in the listL.insert (0,9)Print(l)#[9, 1, 2, 3, 8, 1, 2, 3]#removes an element from the list, defaults to the last element, and returns the value of the element, or you can specify a location to remove the elementS1 =L.pop ()Print(S1)#3Print(l)#[9, 1, 2, 3, 8, 1, 2]S2 =l.pop (0)Print(S2)#9Print(l)#[1, 2, 3, 8, 1, 2]#removes the element that matches the first element in a listL.remove (2)Print(l)#[1, 3, 8, 1, 2]#Reverse list ElementsL.reverse ()Print(l)#[2, 1, 8, 3, 1]#SortL.sort ()Print(l)#[1, 1, 2, 3, 8]#Copy Lists =l.copy ()Print(s)#[1, 1, 2, 3, 8]#Clear Listl.clear ()Print(l)#[]

  

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