Skip to main content
Engineering LibreTexts

8: Lists

  • Page ID
    2941
  • [ "article:topic-guide", "authorname:severancec", "python (language)", "license:ccbyncsa" ]

    • 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.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
    • 8.E: Lists (Exercises)
    • 8.G: Lists (Glossary)