Skip to main content
Chemistry LibreTexts

4.8: List (container)

  • Page ID
    469571
  • \( \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}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

      

    clipboard_ec22a370aac978f5e6f792ed2b7292a1e.pngFigure \(\PageIndex{1}\):The variable molar_Mass is a list. This image was taken from an IDE that displayed strings in red and floating numbers in green (Belford CC0.0)

    Overview

    • Use Brackets
      • Can have multiple types of objects
      • commas separate objects
    • Lists are ordered
      • Support Indexing and Slicing
    • Lists can be nested
    • Lists are mutable (can be modified)
    • Lists are an object class with methods

     

    Methods

    • append
    • count
    • extend
    • insert
    • pop
    • remove
    • reverse
    • sort
    molar_Mass=['hydrogen',1.00784,'helium',4.002602,'lithium', 6.941, 'beryllium', 9.012182]
    print('List Created')
    Hello world!

     

    len(molar_Mass)
            hello world
          

     

    molar_Mass=['hydrogen',1.00784,'helium',4.002602,'lithium', 6.941, 'beryllium', 9.012182]
    print(f"There are {len(molar_Mass)} items in the list molar_Mass")
    Hello world!

    print every other item starting with first item (index number = 0)

    molar_Mass[0::2]
            hello world
          

    print every other item starting with first item (index number = 0) with a range of up to 4 (it does not print item with index =4)

    molar_Mass[0:4:2]
            hello world
          

    Print print every other item starting with second item (index number = 1)

    molar_Mass[1::2]
            hello world
          

    Create three lists

    name_halogen=['fluorine', 'chlorine', 'bromine', 'iodine', 'astatine','tennessine']
    z_halogen=[9,17,35,53,85,117]
    a_halogen=[18.99840316,35.45,79.90,126.9045,209.98715,294.211]
    print('List Created')
    Hello world!

    Print two of the lists, note they are printed as two separate lists

    print(name_halogen,z_halogen)
            hello world
          

    Combine the two lists into one and print it

    print(name_halogen+a_halogen)
            hello world
          

    create a new list and print it

    halogen_masses=name_halogen+a_halogen
    print(halogen_masses)
            hello world
          

     

    print(name_halogen)
    Hello world!

     

    popped_halogen=name_halogen.pop()
    print(name_halogen)
    print(popped_halogen)
    Hello world!

     

    name_halogen.append(popped_halogen)
    print(name_halogen)
            hello world
          

     

    remove_bromine=name_halogen.pop(2)
    print(name_halogen)
    print(remove_bromine)
    name_halogen.insert(2,remove_bromine)
    print(name_halogen)
            hello world
          

     

    halogen=[name_halogen, z_halogen,a_halogen]
    print(f'{halogen}\n')
    
    print(f'{halogen[0]}\n')
    print(f'{halogen[0:][1]}\n')
    print(f'{halogen[0:][0]}\n')
    print(f'{halogen[0][0:]}\n')
    print(f'{halogen[2][0]}\n')
            hello world
          

     

     

    random_list=[22,41,16,444,224]
    print(random_list)
    random_list.reverse()
    print(random_list)
    random_list.sort()
    print(random_list)
    Hello world!

     

     

     

     


    This page titled 4.8: List (container) is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?