Detailed descriptions of List Operation instances in Python programming: [Create, use, update, and delete] and python instances

Source: Internet
Author: User
Tags python list

Detailed descriptions of List Operation instances in Python programming: [Create, use, update, and delete] and python instances

This example describes Python list operations. We will share this with you for your reference. The details are as follows:

# Coding = utf8''' the list type is also a sequential data type. You can access one or more continuous elements by subscript or slicing. A list can contain not only Python standard types, but also user-defined objects as their own elements. The list can contain different types of objects. The list can perform pop, empt, sort, reverse, and other operations. The list can be used to add or remove elements, or to combine with other lists or split a list into several. You can perform insert, update, or remove operations on one or more elements. The main difference between tuples and lists is that the former is not changeable (read-only), and those operations used to update the list cannot be used for the tuples. The list is defined by square brackets ([]). You can also use the factory method list () to create it. You can update one or several elements by specifying an index or index range on the left of the equal sign, or append an element to the list by using the append () method. To delete the elements in the list, use the del statement if you know exactly the index of the elements to be deleted. Otherwise, use the remove () method. You can also use the pop () method to delete and return a specific object from the list. Generally, programmers do not need to delete a list object reference. After a list object has a scope, it is automatically destructed. However, if you want to delete an entire list, you can use the del statement. ''' # Create a list oneList = ["one", 23.6, "two"] # create listtwoList = list ("hello world") using the factory function ") # create an initialized table threeList = [] # print oneList, "\ n", twoList # element in the access list # access print oneList [0] Through indexes, oneList [-1] # access through slice. The default interval is 1 print twoList [0: 2] # access through slice, set the interval to 2 print twoList [] # update the elements in the list # update the oneList [0] = "One" print oneList [0] # update several elements through slice twoList [0: 5] = [1, 2, 3, 4, 5] print twoList [0: 5] # Call the append () method to append the element threeList to the list. append (oneList) threeList. append ("hello") print threeList # delete an element in the list or the List itself # del delete an element in the list print len (twoList) del twoList [5] print len (twoList ), twoList [5] # remove the print len (threeList) threeList element in the list. remove ("hello") print len (threeList), threeList # pop deletes the last element of the list # and saves the deleted element as an object print oneList. pop (), oneList # use a slice to delete the print twoListdel twoList [0: 4] print twoList # delete a list reference print twoListtry: del twoList print twoListexcept Exception, e: print "twoList not exists"

Running result:

For more Python-related content, refer to this topic: Python list) operation Skills summary, Python coding skills summary, Python data structure and algorithm tutorial, Python function usage skills summary, Python string operation skills summary, Python basic and advanced tutorial and Python file and directory operation tips

I hope this article will help you with Python programming.

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.