List operations in Python python

Source: Internet
Author: User

Python's list operation is powerful and convenient (relative to Java)
Simple, regular operation is not said (this is not an introductory tutorial), introduce a few very characteristic examples

Add to
# Append to 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 ']# list and list can also do addition  Li_plus = li + li2print (' li_plus =%s '% li_plus) # output: Li_plus = [1, 2, 3, 4, 5, 6, ' A ', ' B ', ' C ', ' d ', ' e ', ' A ', ' B ', ' C ', ' d ', ' e ']

Delete
# remove elements from list li = [1, 2, 3, 4, 5]li.remove (3) print (' li =%s '% li) # output: Li = [1, 2, 4, 5]
# Delete element according to Index del li[3]print (' li =%s '% li) # output: Li = [1, 2, 4]

Intercept (also called slice operation)
Format: [Start:end:step]. Remember this format and you can play with your imagination.
Li = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# every two elements take a li2 = Li[::2]print (' li2 =%s '% li2) # output: Li2 = [0, 2, 4, 6, 8, 10]# the last element of elements = Li[-1]print (' element =%s ') % Element) # output: element = 10# copy (after copy is a new object) Li2 = List (li) print (' Li2 =%s '% li2) # output: Li2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1 0]

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

Sort
# positive Order Li = [3, 1, 5, 8, 0, 9, 2, 6, 7, 4]li.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

How to create a new list efficiently, based on an existing list
Format: [expression for iter_val in iterable]
# each item in the list takes 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]# only a number greater than 5 takes the square.  An 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 take elements that are not None li = [0, 1, 2, 3, 4, 5, None, 6, 7, 8, 9]li2 = [i-I in Li if I am 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 of li = [[0, 1, 2], [3, 4, 5, 6], [7, 8, 9]]li2 = [j for I in Li-J in i]print (' li2 =%s '% li2) # output: Li2 = [0, 1 , 2, 3, 4, 5, 6, 7, 8, 9]

Elements in a connection list (join)
Li = [' aaa ', ' BBB ', ' CCC ', ' ddd ']li2 = ', '. Join ' print (' li2 =%s '% li2) # output: Li2 = AAA, BBB, CCC, DDD

List operations in Python python

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.