Python Basics-List

Source: Internet
Author: User

Python Basics-List

Common Ways to list:

List.append (obj)---> No return value-operation on the source list

Description
The append () method is used to add a new object at the end of the list.

Parameters

    • Obj--An object added to the end of the list.
In [Wuyi]: L = ['name',' Age','Sex']in [[L.append]:'School') in [53]: lout[53]: ['name',' Age','Sex','School']in [54]: l.append ()---------------------------------------------------------------------------TypeError Traceback (most recent)<ipython-input-54-ed94d4dff9a1>inch<module>()----> 1l.append () typeerror:append () takes exactly one argument (0 given)

Note: You must pass in a parameter. Return to the original list ...

List.count (obj)----> Returns the number of occurrences of the element in the list.

Description

The count () method is used to count the number of occurrences of an element in the list.

Parameters:

    • Obj--The object that is counted in the list.

Instance:

in [58]: lout[58]: ['name',' Age','Sex','School','School','School']in [(+]: L.count ('School') out[59]: 3In [Max]: L.count ('name') out[60]: 1

List.extend (seq)---> No return value-operation on the source list

Description

The Extend () function is used to append multiple values from another sequence at the end of the list (the original list is expanded with a new list).

Parameters:

    • SEQ--List of elements.

Instance

in [80]: lout[80]: ['name',' Age']in [81]: llout[81]: ['Kevin','Bob']in [82]: L.extend (LL) in [83]: lout[83]: ['name',' Age','Kevin','Bob']in [(+]: l.extend ('Str') # string in [85]: lout[85]: ['name',' Age','Kevin','Bob','s','T','R']in [[L.extend]:) # tuple in [87]: lout[87]: ['name',' Age','Kevin','Bob','s','T','R', 1, 2] in [[L.extend]: ({'Sex': 1}) # Note that when the argument is a dictionary, only the key values in the dictionary are added to the list. in [89]: lout[89]: ['name',' Age','Kevin','Bob','s','T','R', 1, 2,'Sex']

List.index (obj)-----> returns the index value of the object-throws an exception if no object is found .

Description

The index () function is used to find the index position of the first occurrence of a value from the list.

Parameters:

    • Obj--the object to find.

Instance:

  

in [90]: lout[90]: ['name',' Age','Kevin','Bob','s','T','R', 1, 2,'Sex']in [[L.index]:'name') out[91]: 0In [[L.index]:' Age') out[92]: 1In ["L.index" ('XXX'# throws an exception when it doesn't exist---------------------------------------------------------------------------ValueError Traceback (most recent)<ipython-input-93-04929cb153a2>inch<module>()----> 1 L.index ('XXX') ValueError:'XXX'  is  not inchList

List.insert (index,obj)---> No return value to manipulate the source list

Description

The insert () function is used to insert the specified object into the specified position of the list.

Parameters:

  • Index--the indexed position at which the object obj needs to be inserted.
  • Obj--To insert an object from the list.

Instance:

In [the]: L.insert (1,'x') in [106]: lout['x ', 2, 3, 4, 5, 6]

List.pop (Index)-----> Returns the object that we moved through this method

Description

The pop () function removes an element from the list (the last element by default) and returns the value of the element.
Parameters

    • Index--optional parameter, to remove the index value of the list element, cannot exceed the total length of the list, defaults to Index=-1, and deletes the last list value.

return value:

This method returns the element object that is removed from the list.

Instance

in [108]: lout['x', 2, 3, 4, 5, 6]in [109]: L.pop () out[109]: 6  in [+]: lout['x', 2, 3, 4, 5]in [111]: L.pop (1) out[ ' x ' In [lout[]:112]: [1, 2, 3, 4, 5]

List.remove ()----> This method does not return a value but removes the first occurrence of one of two values.

 

Description: The

Remove () function is used to remove the first occurrence of a value in the list.

Parameter:

  • obj-the object to be removed from the list. When there are multiple objects in the list, the default is to move the first occurrence

Instance

 in [120 120]: [1, 2, 3, 4, 5, 1, 2, 3]in [ 121]: L.remove (1 122 Span style= "COLOR: #000000" >]: lout[ 122]: [2, 3, 4, 5, 1, 2, 3]in [ 123]: L.remove () ------------------------------------------------------------------------ ---typeerror Traceback (most recent call last)  <IPYT hon-input-123-7d9f45228ac5> in  <module>  () ----> 1 L.remove () Typeerror:remove () takes Exactly one argument (0 given)  

 

 

List.reverse ()---The Elements of the > list to reverse-sort.

Description

The reverse () function is used to reverse the elements in the list.

Instance:

In [the]: lout[[2, 3, 4, 5, 1, 2, 3]in [126]: L.reverse () in [127]: lout[127]: [3 , 2, 1, 5, 4, 3, 2]

List.sort (Key,reserve)

Description

The sort () function is used to sort the original list, and if a parameter is specified, the comparison function specified by the comparison function is used.

Parameters:

    • Key--the element used for comparison, with only one parameter, the parameters of the function are taken from an iterative object, specifying that an element in the object can be iterated to sort.
    • Reverse--collation,reverse = True Descending, reverse = False Ascending (default).

Instance

in [131]: lout[131]: [2, 4, 3, 1, 5]in [132]: L.sort () in [133]: lout[133]: [1, 2, 3, 4, 5]in [134]: L.sort? Type:builtin_function_or_methodstring form:<built-inchMethod sort of List object at 0x7fa3d9fe4748>Docstring:l.sort (Key=none, Reverse=false), None--stable sort *in place*In [135]: LL = [' This',' is','a','Demo']in [136]: Ll.sort (key=Len) in [137]: llout[137]: ['a',' is',' This','Demo']in [138]: lout[138]: [1, 2, 3, 4, 5]in [139]: L.sort (reverse=True) in [140]: lout[140]: [5, 4, 3, 2, 1]

List.copy ()----Returns the new list after replication.

Description

The copy () function is used to copy the list, similar to a[:].

Instance:

In [141]: lout[141]: [5, 4, 3, 2, 1]in [142]: LC = l.copy () in [143]: lcout[143]: [5, 4 , 3, 2, 1]
In [146]: LZ = l[:]in [147]: lzout[147]: [5, 4, 3, 2, 1]

List.clear ()

Description

The clear () function is used to empty the list, similar to del a[:].

Instance:

In [153]: lout[153]: [5, 4, 3, 2, 1]in [154]: lcout[154]: [5, 4, 3, 2, 1]in [155]: L.clear () in [156]: lout[156]: []in [del  lc[:]in [158]: lcout[ 158]: []

Operation of the list

... Pending update

Python Basics-List

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.