Skip to main content
Engineering LibreTexts

13.2: Introduction - lists

  • Page ID
    26338
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The second item of interest is the list. Lists are sequences like tuples, but unlike tuples, lists are mutable, that is, the elements within a list maybe changed. Tuples are best thought of as a collection of constants in comparison. Lists are defined using square brackets [] instead of using parentheses () like a tuple. Like tuples and strings, lists are accessed by using square brackets []. Lists can be sliced just like tuples.

    T = (12,43,17)      # This is a tuple definition
    L = [12,43,17]      # This is a list definition
    print( T[0] )       # print first element of tuple
    print( L[0] )       # print first element of list
    

    Note that it is not obvious if the item is a tuple or list when it is accessed. It is only obvious when it is defined. Also note that the entire contents of a list may be printed out as follows:

    print( L )
    

    The line above produces the entire list contents within brackets, each element separated by a comma:

    [12,43,17]
    

    Because lists are mutable, contents may be changed. Using the prior example:

    L[1] = 4.3     # This is legal
    T[1] = 4.3     # This is not legal
    

    Because lists are mutable, there are a number of functions and methods that may be applied to them (think of methods as functions that apply to that particular object). One useful function is len(). This will let you determine how many items are in the list. Two useful methods are sort() and append(). sort() will rearrange the items in the list into ascending or descending order while append() will allow you to add new items to the list. Methods are thought of as belonging to the object in question so they are accessed in a slightly different manner from functions. In the example lines below, assume that the list L exists. The function call for len() should look familiar, however, note the new syntax for the two methods:

    n = len(L)
    L.append(x)
    L.sort()
    

    The first line determines how many elements are in the list L and assigns the result to the variable n. The second line adds a new element to the list L with the value of x. The third line sorts the list L from smallest to largest (to get the reverse ordering, use the optional reverse argument, as in L.sort(reverse=True) instead).


    This page titled 13.2: Introduction - lists is shared under a not declared license and was authored, remixed, and/or curated by James M. Fiore.

    • Was this article helpful?