Change language

List of methods in Python

|

This article is a continuation of the following articles:
list.append (element)

# 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 ("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.

    Shop

    Learn programming in R: courses

    $

    Best Python online courses for 2022

    $

    Best laptop for Fortnite

    $

    Best laptop for Excel

    $

    Best laptop for Solidworks

    $

    Best laptop for Roblox

    $

    Best computer for crypto mining

    $

    Best laptop for Sims 4

    $

    Latest questions

    NUMPYNUMPY

    Common xlabel/ylabel for matplotlib subplots

    12 answers

    NUMPYNUMPY

    How to specify multiple return types using type-hints

    12 answers

    NUMPYNUMPY

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    12 answers

    NUMPYNUMPY

    Flake8: Ignore specific warning for entire file

    12 answers

    NUMPYNUMPY

    glob exclude pattern

    12 answers

    NUMPYNUMPY

    How to avoid HTTP error 429 (Too Many Requests) python

    12 answers

    NUMPYNUMPY

    Python CSV error: line contains NULL byte

    12 answers

    NUMPYNUMPY

    csv.Error: iterator should return strings, not bytes

    12 answers

    News


    Wiki

    Python | How to copy data from one Excel sheet to another

    Common xlabel/ylabel for matplotlib subplots

    Check if one list is a subset of another in Python

    sin

    How to specify multiple return types using type-hints

    exp

    Printing words vertically in Python

    exp

    Python Extract words from a given string

    Cyclic redundancy check in Python

    Finding mean, median, mode in Python without libraries

    cos

    Python add suffix / add prefix to strings in a list

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    Python - Move item to the end of the list

    Python - Print list vertically