Python list usage, python list

Source: Internet
Author: User
Tags python list

Python list usage, python list

#! Usr/bin/env python
#-*-Coding: UTF-8 -*-
# The following methods are all run in python2.7.x. Please execute them in the print (put in parentheses) by those above 3. x)
# List methods:
Li = [] # define an empty list;
Li. append (1) # Add a constraint to the end of the list;
Print li
Li2 = [2, 3, 3, 4, 5, 6]
Li. extend (li2) # merge the specified li2 list to the end of the li list;
Print li
Li. insert (6, 0) # insert element 0 at 6th locations in the subscript [also called Index] of li (corresponding to the elements in the list one by one, accumulating from 0 by default)
Print li
Li. remove (4) # Delete element 4 from the list
Print li
S = li. pop () # Delete the last element and return its value
Print li
Print "Return Value pop =", s
A = li. index (1) # locate the position/subscript/index of 1 in the list Li and return its value
Print "1 in the list:",
C = li. count (3) # count the number of times that li appears in the list and return its value
Print "3 times in the list:", c
Li. sort () # elements in the sorting list
Print li
Li. reverse () # reverse the element in the list
Print li
# Print the following output:

 

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.