Skip to main content
Chemistry LibreTexts

6: List

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

      

    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 6: List is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?