Python list operation examples

Source: Internet
Author: User
Tags python list

Copy codeThe Code is as follows:
Python 3.3.4 (v3.3.4: 7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license ()" for more information.
>>> Cast = ["cleese", "palin", "jones", "idle"]
>>> Print (cast)
['Cleese ', 'palin', 'Jones', 'idle']
>>> Print (len (cast) # display the number of data items
>>> Print (cast [1]) # display the values of the first data item in the list
>>> Cast. append ("gilliam") # Add a data item to the end of the list.
>>> Print (cast)
['Cleese ', 'palin', 'Jones', 'idle', 'gillim']
>>> Cast. pop () # delete data items at the end of the list
>>> Print (cast)
['Cleese ', 'palin', 'Jones', 'idle']
>>> Cast. extend (["gilliam", "chapman"]) # Add a data item set at the end of the list.
>>> Print (cast)
['Cleese ', 'palin', 'Jones', 'idle', 'gillim', 'chapman']
>>> Cast. remove ("chapman") # delete a specified data item
>>> Print (cast)
['Cleese ', 'palin', 'Jones', 'idle', 'gillim']
>>> Cast. insert (0, "chapman") # add data items to a specified position
>>> Print (cast)
['Chapman ', 'cleese', 'palin', 'Jones ', 'idle', 'gillim']

The following describes the application and logic of defining a def function, isinstance () function, for in, if else, and so on.
Copy codeThe Code is as follows:
Movies = ["the holy grail", 1975, "terry jone & terry gilliam", 91,
["Graham chapman ",
["Michael palin", "john cleese", "terry gilliam ",
"Eric idle", "terry jones"]
Def print_lol (the_list): # define a function
For each_item in the_list: # for in loop iteration processing list, from the starting position of the list to the end
If isinstance (each_item, list): # isinstance () Checks each item in each_item
# Is it list type?
Print_lol (each_item) # If yes, call the print_lol function.
Else: print (each_item) # If not, output this item

Print_lol (movies) # Call the function in the movies list
If the else statement is not aligned, an error is reported.

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