List of methods in Python

File handling | Python Methods and Functions

This article is a continuation of the following articles:

# Adds a list item as a list value

List = [ 'Mathematics' , ' chemistry' , 1997 , 2000 ]

List . append ( 20544 )

print ( List )

Output:

 ['Mathematics',' chemistry', 1997, 2000, 20544] 

  • insert (): inserts elements at the specified position.
    Syntax :
     list.insert (& lt; position, element) 

    Note: the specified position must be within the range of the List, since in this case from 0 to 4, otherwise an IndexError will be thrown.

    List = [ 'Mathematics' , ' chemistry' , 1997 , 2000 ]

    # Insert value 10087 into index 2

    List . insert ( 2 , 10087

    pri nt ( List

    Output:

     ['Mathematics',' chemistry', 10087, 1997, 2000, 20544] 
  • extended (): adds content in List2 to the end of List1.
    Syntax :
    List1.extend(List2)

    List1 = [ 1 , 2 , 3 ]

    List2 = [ 2 , 3 , 4 , 5 ]

     
    # Add List2 to List1
    List1.extend (List2) 

    print (List1)

     
    # Add List1 to List2 now
    List2.extend (List1) 

    print (List2)

    Output:

     [1 , 2, 3, 2, 3, 4, 5] [2, 3, 4, 5, 1, 2, 3, 2, 3, 4, 5] 
  • List (), count (), index (), min () and max () functions of the list

    • sum (): calculates the sum of all the elements in the list.
      Syntax :
       sum (List) 

      List = [ 1 , 2 , 3 , 4 , 5 ]

      print ( sum ( List ))

      Output:

       15 

      What happens if a numeric value is not used by a parameter?
      Sum is calculated for numeric values ​​only, throws TypeError otherwise.
      See an example:

      List = [ 'gfg' , ' abc' , 3 ]

      print ( sum ( List ))

      Output:

       Traceback (most recent call last): File "", line 1, in sum (List) TypeError: unsupported operand type ( s) for +: 'int' and' str' 
    • count (): calculates the total occurrence of the given List element.
      Syntax :
      List.count(element)

      List = [ 1 , 2 , 3 , 1 , 2 , 1 , 2 , 3 , 2 , 1 ]

      print ( List . count ( 1 ))

      Output:

       4 
    • length: calculates the total length of the list.
      Syntax :
      len(list_name)

      List = [ 1 , 2 , 3 , 1 , 2 , 1 , 2 , 3 , 2 , 1 ]

      print ( len ( List ))

      Output:

       10 
    • index (): returns the index of the first occurrence. The starting and ending indexes are optional.
      Syntax :
      List.index(element [,start [,end>

      List = [ 1 , 2 , 3 , 1 , 2 , 1 , 2 , 3 , 2 , 1 ]

      print ( List . index ( 2 ))

      Output:

       1 

      Another example:

      List = [ 1 , 2 , 3 , 1 , 2 , 1 , 2 , 3 , 2 , 1 ]

      print ( List . index ( 2 , 2 ))

      Output:

       4 

      List = [ 1 , 2 , 3 , 1 , 2 , 1 , 2 , 3 , 2 , 1 ]

       
      # check from index 2 to 3.

      print ( List .index ( 2 , 2 , 4 )) 

      Output:

       Traceback (most recent call last): File "", line 1 , in List.index (2,2,4) ValueError: tuple.index (x): x not in tuple 
    • min (): calculates the minimum of all list items.
      Syntax :
      min(List)

      List = [ 2.3 , 4.445 , 3 , 5.33 , 1.054 , 2.5 ]

      print ( min ( List ))

      Output:

       1.054 
    • max (): calculates the maximum of all list items.
      Syntax :
      max(List)

      List = [ 2.3 , 4.445 , 3 , 5.33 , 1.054 , 2.5 ]

      print ( max ( List ))

      Output:

       5.33 

    sort () and reverse () functions

    • reverse (): sort the given data structure (both tuple and list) in ascending order. The key and reverse_flag are optional, and reverse_flag is False if nothing is passed through sorted ().
      Syntax :
       sorted ([list [, key [, Reverse_Flag]]]) list.sort ([key, [Reverse_flag]]) 

      List = [ 2.3 , 4.445 , 3 , 5.33 , 1.054 , 2.5 ]

       
      # Reverse flag set to True

      List . sort (reverse = True

        
      # List.sort (). Reverse (), reverses the sorted list

      print ( List

      Output:

       [5.33, 4.445, 3, 2.5, 2.3, 1.054] 

      List = [ 2.3 , 4.445 , 3 , 5.33 , 1.054 , 2.5 ]

      sorted ( List )

      print ( List )

      Output:

       [1.054, 2.3, 2.5, 3, 4.445, 5.33] 

    Deleting list items

    To delete one or multiple elements, i.e. remove an element, many built-in functions can be used like pop () and remove () and keywords like del.