Python learns some of the actions of the list in -29.python

Source: Internet
Author: User

in Keyword:

Note that this is a keyword that is used to determine whether an element exists in the collection.

1 list = ['a','b','C' ]2print('a' in list) 3 Print ('f' in list)

True and False will be output in turn

+

1 lista=['a','b']2 listb=['  c','d']3 listc=lista+  Listb4print(LISTC)#  a b c D

Equivalent to a string of connections, LISTC is a new copy.

Append method:

 1  list = [ " a   ", "  b   ", "  c   ", "  d   ", "  e       '  

The Append method takes only one parameter and appends the argument to the end of the list, noting that the Append method returns none, so print (List.append (' F ')) is wrong. The Append method is equivalent to the Add method in C #.

Extend method:

1List = ['a','b','C','D','e']    2List.extend (['F','g'])3 Print(list)

The Extend method also has only one parameter, and requires that the parameter object be traversed, the extend method iterates through the parameter collection, adds its elements sequentially to the list, and similarly, the Extend method returns none.

Insert Method:

 1  list = [ " a   ", "  b   ", "  c   ", "  d   ", "  e       '  

The Insert method accepts two parameters, where the first argument is of type int, indicating where the new element is inserted. The Insert method returns none. This method is consistent with the Insert method of list in C #.

Count Method:

 1  list = [ " a   ", "  b   ", "  c   ", "  d   ", "  e   ", "  a      Print  (List.count ( " a   ' ) 

The Count method accepts a parameter, and the query list is medium to the number of elements in the parameter, consistent with the Count method of LINQ in C #.

Index method:

1List = ['a','b','C','D','e']2 Print(List.index ('C'))#Output 23 Print(List.index ('F'))#Generate ValueError Error

The index method queries the position of the element in the list, and it is worth noting that if the element does not exist, it will produce an exception, which is not as good as the IndexOf method in C # returns-1.

del Keyword:

Note that this is the keyword first.

 1  list = [ " a   ", "  b   ", "  c   ", "  d   ", "  e     del  list[1  3  print  (list) #   a C D e  

Remove the element of the list from the list, which is the element that removes list[1], which is B.

Remove method:

1List = ['a','b','C','D','e','a']2List.remove ('a')3 Print(list)

The Remove method removes the conforming element from the list one time . In other words, the result is B C D e a. If the element does not exist, an exception is generated.

Pop method:

1 list = ['a','b','C', ' D ','e']2 var = list.pop ()3 Print (list)

The Pop method deletes the last element in the list and returns the element. If the list is empty, an exception is generated.

Python learns some of the actions of the list in -29.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.