List operations in Python and list operations in Python

Source: Internet
Author: User

List operations in Python and list operations in Python

Python list operations are powerful and convenient (compared with Java)
Simple and regular operations will not be mentioned (this is not an introductory tutorial). We will introduce several examples with special features.

Add
# Append to the end (append) li = [1, 2, 3, 4, 5] li. append (6) print ('Li = % s' % li) # output: li = [1, 2, 3, 4, 5, 6] # append a list (extend) li2 = ['A', 'B', 'C', 'D', 'E'] li. extend (li2) print ('Li = % s' % li) # output: li = [1, 2, 3, 4, 5, 6, 'A ', 'B', 'C', 'D ', 'E'] # Add li_plus = li + li2print ('li _ plus = % s' % li_plus) to the list and list # output: li_plus = [1, 2, 3, 4, 5, 6, 'A', 'B', 'C', 'D', 'E', 'A', 'B', 'B', 'C ', 'D', 'E']

Delete
# Delete the element li = [1, 2, 3, 4, 5] li in the list. remove (3) print ('Li = % s' % li) # output: li = [1, 2, 4, 5]
# Delete the element del li [3] print ('Li = % s' % li) based on the index # output: li = [1, 2, 4]

 

Intercept (also called slice Operation)
Format: [start: end: step]. Remember this format and you will be able to make full use of your imagination.
Li = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Take a li2 = li [: 2] print ('li2 = % s' % li2) for every two elements # output: li2 = [0, 2, 4, 6, 8, 10] # last element = li [-1] print ('element = % s' % element) # output: element = 10 # copy (a new object after copying) li2 = list (li) print ('li2 = % s' % li2) # output: li2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Extract and delete (pop)
Li = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # extract and delete the last element li. pop () # equivalent to li. pop (-1) print ('Li = % s' % li) # output: li = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # extract and delete the first element (or the nth element) li. pop (0) print ('Li = % s' % li) # output: li = [1, 2, 3, 4, 5, 6, 7, 8, 9]

 

Sort
# Li = [3, 1, 5, 8, 0, 9, 2, 6, 7, 4] li in the forward order. sort () print ('Li = % s' % li) # output: li = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # reverse li = [3, 1, 5, 8, 0, 9, 2, 6, 7, 4] li. sort (reverse = True) print ('Li = % s' % li) # output: li = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] # Flip (not in reverse order) li = [3, 1, 5, 8, 0, 9, 2, 6, 7, 4] li. reverse () print ('Li = % s' % li) # output: li = [4, 7, 6, 2, 9, 0, 8, 5, 1, 3]

List Parsing

Efficiently create a new list based on the existing list
Format: [expression for iter_val in iterable]
# Square li = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] li2 = [I ** 2 for I in li] print ('li2 = % s' % li2) # output: li2 = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] # obtain the square of a number greater than 5. The expression can also be an if statement li = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] li2 = [I ** 2 if I> 5 else I for I in li] print ('li2 = % s' % li2) # output: li2 = [0, 1, 2, 3, 4, 5, 36, 49, 64, 81]
Format: [expr for iter_var in iterable if cond_expr]
# Only elements other than None, li = [0, 1, 2, 3, 4, 5, None, 6, 7, 8, 9] li2 = [I for I in li if I is not None] print ('li2 = % s' % li2) # output: li2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Format: [expr for iter_var in iterable for var in iter_var]
# Expand a two-dimensional list li = [0, 1, 2], [3, 4, 5, 6], [7, 8, 9] li2 = [j for I in li for j in I] print ('li2 = % s' % li2) # output: li2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Join)
Li = ['aaa', 'bbb ', 'ccc', 'ddd '] li2 = ','. join (li) print ('li2 = % s' % li2) # output: li2 = aaa, bbb, ccc, ddd
 

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.