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 lists in Python, highlighting their nature as sequences of values of various types, distinguishable from strings. It notes lists are created with square brackets and can comprise mixed data types, including nested lists. An empty list is shown as empty brackets, and examples illustrate assigning lists to variables, showcasing their versatility with integers and strings.
- 8.2: Lists are mutable
- This page explains accessing and manipulating Python list elements using indices, emphasizing their mutability. It covers index numbering (starting at 0 and including negative indices), the possibility of using any integer as an index, and the potential for IndexErrors with out-of-bounds access. Additionally, it describes the 'in' operator for checking element existence in lists.
- 8.3: Traversing a List
- This page explains how to use a for loop in Python to traverse lists, with an emphasis on reading and updating elements using `range` and `len`. An example shows how to double each list element. It also mentions that a for loop does not execute over an empty list, and when counting elements, nested lists are considered as single elements, affecting the total length.
- 8.4: List operations
- This page explains the use of the + operator for concatenating lists, resulting in a new list with combined elements, and the * operator for repeating a list a specified number of times. For example, [1, 2, 3] + [4, 5, 6] yields [1, 2, 3, 4, 5, 6], while [0] * 4 gives [0, 0, 0, 0].
- 8.5: List Slices
- This page explains the Python slice operator for lists, highlighting its functionality in extracting parts of a list. It describes how omitting indices can produce different results, such as starting from the beginning, going to the end, or creating a full copy. Additionally, it mentions that lists are mutable, allowing for copying before modifications, and notes that the slice operator can be used in assignments to update multiple list elements.
- 8.6: List Methods
- This page discusses Python list manipulation methods, highlighting `append` for adding single elements and `extend` for adding multiple elements from another list. It also addresses the `sort` method, which organizes list elements in ascending order. Most list methods modify the existing list and return None, cautioning users against reassigning results that may lead to unexpected behavior.
- 8.7: Deleting Elements
- This page discusses different methods for deleting elements from a list in Python. The `pop` method removes an item at a given index and can return it, while the `del` operator deletes elements without returning any value. The `remove` method focuses on removing elements by their value. Additionally, `del` can be used to delete multiple elements through slice indexing. Each method alters the list in its way.
- 8.8: Lists and Functions
- This page discusses Python's built-in list functions for efficiently calculating statistics such as length, maximum, minimum, and sum. It contrasts two methods for calculating the average of user inputs: one using total and count without lists, and a more efficient list-based approach that simplifies average calculation using sum() and len() after data collection. The text highlights the advantages of utilizing lists in Python programming.
- 8.9: Lists and Strings
- This page explains the differences between strings and lists, emphasizing that a string is a sequence of characters and a list is a sequence of values. It describes methods for converting a string to a list of characters using the list function, and breaking a string into words with the split method, which can take a delimiter. It also discusses the join method for merging a list of strings back into a single string, highlighting the flexibility in handling strings and lists in programming.
- 8.10: Parsing lines
- This page explains how to extract specific information from lines in a file starting with "From ". It details a program that identifies these lines, splits them, and prints the third word representing the day of the week. The example highlights the use of the split method and a simplified if statement, showing varied outputs for different days. The content also suggests upcoming lessons on advanced parsing techniques.
- 8.11: Objects and Values
- This page clarifies the difference between object identity and value in Python. It explains that two variables can contain the same value but may refer to different objects, especially in the case of lists. The 'is' operator verifies if two variables point to the same object. The text stresses that "object" and "value" are not synonymous, underscoring the significance of this distinction in programming.
- 8.12: Aliasing
- This page explains that in Python, assigning one variable to another creates an alias, meaning both refer to the same object. Changes to a mutable object will affect both variables, leading to potential issues. To avoid problems, it's recommended to avoid aliasing with mutable objects. However, with immutable objects like strings, aliasing is less concerning since they don't change.
- 8.13: List arguments
- This page explains how functions handle lists in Python, emphasizing that passing a list gives a reference, allowing modifications to affect the original list. For example, the `delete_head` function modifies the list, while operations like slicing create new lists without changing the original. It highlights the importance of understanding these behaviors when writing list-modifying functions and provides exercises to practice creating such functions.
- 8.14: Debugging
- This page highlights common pitfalls in using lists in Python, stressing the need to understand list methods and the impact of in-place modifications. It advises on creating copies to prevent aliasing issues and discusses error handling in file processing, particularly with blank lines. The text recommends using print statements for debugging and implementing guardian conditions to avoid errors. Lastly, it encourages practicing code guards and refining code logic through exercises.
- 8.E: Lists (Exercises)
- This page summarizes three programming exercises: Exercise 4 focuses on reading a file, extracting unique words, and printing them alphabetically; Exercise 5 involves analyzing email data to count occurrences of email addresses from lines beginning with "From"; Exercise 6 prompts users to input numbers, storing them to later calculate and display the maximum and minimum values upon receiving "done."
- 8.G: Lists (Glossary)
- This page defines key programming concepts including aliasing, delimiter, element, equivalent, index, identical, list, list traversal, nested list, object, and reference, explaining their roles and relationships in programming.