Skip to main content
Engineering LibreTexts

8: Lists

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

    • 8.1: A list is a sequence
      Like a string, a list is a sequence of values. In a string, the values are characters; in a list, they can be any type. The values in list are called elements or sometimes items.
    • 8.2: Lists are mutable
      The syntax for accessing the elements of a list is the same as for accessing the characters of a string: the bracket operator. The expression inside the brackets specifies the index. Remember that the indices start at 0:
    • 8.3: Traversing a List
      The most common way to traverse the elements of a list is with a for loop.
    • 8.4: List operations
      The + operator concatenates lists.
    • 8.5: List Slices
      The slice operator also works on lists.
    • 8.6: List Methods
      Python provides methods that operate on lists. For example, append adds a new element to the end of a list:
    • 8.7: Deleting Elements
      There are several ways to delete elements from a list. If you know the index of the element you want, you can use pop.
    • 8.8: Lists and Functions
      There are a number of built-in functions that can be used on lists that allow you to quickly look through a list without writing your own loops:
    • 8.9: Lists and Strings
      A string is a sequence of characters and a list is a sequence of values, but a list of characters is not the same as a string. To convert from a string to a list of characters, you can use list.
    • 8.E: Lists (Exercises)
    • 8.G: Lists (Glossary)
    • 8.10: Parsing lines
      Usually when we are reading a file we want to do something to the lines other than just printing the whole line. Often we want to find the "interesting lines" and then parse the line to find some interesting part of the line.
    • 8.11: Objects and Values
      To check whether two variables refer to the same object, you can use the is operator.
    • 8.12: Aliasing
      The association of a variable with an object is called a reference. In this example, there are two references to the same object.
    • 8.13: List arguments
      When you pass a list to a function, the function gets a reference to the list. If the function modifies a list parameter, the caller sees the change. For example, delete_head removes the first element from a list.
    • 8.14: Debugging


    This page titled 8: Lists is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Chuck Severance via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?