List of Python operations

Source: Internet
Author: User

#Summary of list operations functionsPrint("Summary of list operations functions") List_demo= [' First','Second','Thrid','Fourth']#copy list_demo list nameList =list_demo[:]Print("The original list is:", list)Print("-----------------------------")Print("The first element of the output list:", list[0])Print("The last element of the output list:", list[-1])Print("elements from 2 start to 3rd output list:", List[1:3])Print("elements from 2 start to end of output list:", list[1:])Print("-----------------------------")#insert element to end of listList =List_demo[:]list.append ("Hello")Print("Insert an element to the end of the list:", list)#inserts an element at the specified position in the listList =List_demo[:]list.insert (1,"Hello")Print("inserts the element at the specified position in the list:", list)Print("-----------------------------")#remove an element from a specified position in a listList =list_demo[:]delList[1]Print("Delete the element at the specified position in the list:", list)#removes the element from the specified position in the list and recordsList =List_demo[:]popone= List.pop (1)Print("Delete the element at the specified position in the list and record:", List,"the deleted elements are:", Popone)#Delete an element of a specified value in a listList =List_demo[:]list.remove (" First")Print("to delete data from a specified value in a list:", list)Print("-----------------------------")#List parsing: Merging the code for loops and expressions into one lineList = [value**2 forValueinchRange (1, 5)]Print("List parsing results:", list)Print("-----------------------------")#checks whether the specified element is in the list: in or not. List =list_demo[:]if " First" inchlist:Print("judging ' first ' in the list")Print("-----------------------------")#determine if there is a value in the listiflist:Print("there is a value in the Judgment list. ")Else:    Print("The judgment list is empty. ")

Operation Result:

List Operation function Summary The original list is: [' first ', ' second ', ' Thrid ', ' fourth ']-----------------------------output list, the last element of the list: Fourth elements from 2 start to 3rd output list: [' Second ', ' Thrid '] from 2 to the end of the output list of elements: [' second ', ' Thrid ', ' fourth ']-------------------------- ---list insert element to end: [' first ', ' second ', ' Thrid ', ' fourth ', ' hello '] list in the specified position of the insertion element: [' first ', ' Hello ', ' second ', ' Thrid ', ' fourth ' ']-----------------------------Delete the element at the specified position in the list: [' First ', ' Thrid ', ' fourth '] Delete the element at the specified position in the list and record: [' first ', ' Thrid ', ' fourth The deleted element is: Second delete data from the specified value in the list: [' Second ', ' Thrid ', ' fourth ']-----------------------------list parse Result: [1, 4, 9,]--------- --------------------judge that ' first ' has a value in the list-----------------------------judgment list.

List of Python operations

Related Article

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.