8: Lists
- Page ID
- 122377
\( \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}\)- 8.1: A list is a sequence
- This page explains that lists are versatile sequences of values that can contain different types of elements, created with square brackets. They allow for mixed types and even nested lists, with empty lists shown as empty brackets. The ability to assign list values to variables enhances data organization and management, with examples illustrating lists of integers, strings, and mixed types.
- 8.2: Lists are mutable
- This page explains that lists use bracket notation for element access with 0-based indexing and support mutability, allowing element modifications. It highlights the use of negative indices, the "in" operator for membership tests, and mentions that accessing an invalid index raises an IndexError. An example illustrates element reassignment in a list.
- 8.3: Traversing a List
- This page explains how to traverse and manipulate lists in Python with for loops. It demonstrates reading elements using a simple for loop and updating elements with indices via `range` and `len`. The text includes an example of doubling list elements, mentions that a for loop over an empty list does not run, and notes that nested lists count as single elements for length purposes.
- 8.4: List operations
- This page explains how to use the + and * operators for list manipulation in Python. The + operator concatenates two lists, merging them into one, while the * operator duplicates a list a specified number of times. Examples include merging [1, 2, 3] and [4, 5, 6] to yield [1, 2, 3, 4, 5, 6], and repeating [0] four times to create [0, 0, 0, 0].
- 8.5: List Slices
- This page explains the slice operator's functionality in lists, enabling users to retrieve specific segments, create full copies, and update multiple elements. It highlights the mutability of lists, recommending copies before modifications for safety.
- 8.6: List Methods
- This page outlines Python's list manipulation methods, focusing on `append` for adding single elements, `extend` for merging lists, and `sort` for ordering elements. It highlights that these methods alter the original list and return None, warning against reassigning the result as it can cause unexpected behavior.
- 8.7: Deleting Elements
- This page explains methods for deleting elements from a Python list: the `pop` method removes an element by index and returns it; the `del` operator removes without returning the value; and the `remove` method deletes by value. It also describes how to use `del` with slice indexing to remove multiple elements.
- 8.8: Lists and Functions
- This page explores Python's built-in functions for list operations, like len(), max(), min(), and sum(). It shows how to compute the average of user-entered numbers first using a loop with variables for totals and counts and then by appending numbers to a list. This method highlights the capabilities of lists and built-in functions to streamline code, calculating the average more efficiently by dividing the sum of the list by its length.
- 8.9: Lists and Strings
- This page explains the differences between strings and lists, highlighting that a string is a sequence of characters and a list is a sequence of values. It discusses how to convert a string to a list using the `list` function and how to split a string into words using the `split` method. Additionally, it describes the `join` method, which concatenates a list of strings into a single string, utilizing a specified delimiter, such as spaces or an empty string.
- 8.10: Parsing lines
- This page explains how to extract specific information from lines in a file, particularly those starting with "From ". It details a program that opens a file, checks each line, and prints the third word (day of the week) using the split method. A shorthand form of the if statement is mentioned for improved efficiency. The program outputs days like "Sat" and "Fri," with a note that more complex data extraction techniques will be discussed later.
- 8.11: Objects and Values
- This page explains the difference between variables that refer to the same object and those that refer to different objects with the same value in Python. It illustrates this with examples of strings and lists, highlighting that two identical lists are distinct objects while two strings can reference the same object. The `is` operator is mentioned for checking object identity.
- 8.12: Aliasing
- This page explains the implications of assigning one variable to another, highlighting the concept of referencing or aliasing. It emphasizes the importance of mutability, noting that changes to mutable objects like lists affect all references. This can cause errors, suggesting it's safer to avoid aliasing mutable objects. Conversely, aliasing immutable objects such as strings is generally safe, as it does not alter functionality.
- 8.13: List arguments
- This page explains how lists are passed by reference in functions, allowing for modifications to affect the original list. It discusses the `delete_head` function, which removes the first element, and notes that operations like slicing result in new lists. It also highlights the incorrect approach of the `bad_delete_head` function and introduces the `tail` function, which preserves the original list.
- 8.14: Debugging
- This page discusses the risks of using lists and mutable objects in programming, especially in Python. It highlights the distinction between list and string methods, and the need for consistent coding practices. Copying lists during modifications is advised, along with safeguards against errors when processing files. The text recommends debugging techniques, such as adding print statements and using guardian code, to prevent index errors and enhance code reliability.
- 8.E: Lists (Exercises)
- This page outlines programming exercises that involve file handling and data processing. Exercise 4 focuses on reading a file to create a sorted list of unique words. Exercise 5 analyzes an email dataset to count occurrences of a specific word in lines that start with "From". Lastly, Exercise 6 requires user input of numbers, storing them in a list and subsequently determining the maximum and minimum values upon user completion.
- 8.G: Lists (Glossary)
- This page explains essential programming concepts: aliasing (multiple variables to one object), delimiter (where to split strings), element (value in a list), equivalent (same value), index (identifier for list elements), identical (same object), list (sequence of values), list traversal (accessing each element), nested list (list within another list), object (variable's type and value), and reference (link between variable and value).