Programming with Python · free preview

Storing Multiple Values in Lists

Explain what a list is. Create and index lists of simple values. Change the values of individual elements. Append values to an existing list. Reorder and slice list elements. Create and manipulate nested lists.

1. Overview

Overview

2. Python lists

Python lists

3. Ch-Ch-Ch-Ch-Changes

Ch-Ch-Ch-Ch-Changes

4. Nested Lists

Nested Lists

5. Heterogeneous Lists

Heterogeneous Lists

6. Slicing From the End

Slicing From the End

7. Non-Continuous Slices

Non-Continuous Slices

8. Overloading

Overloading

9. Visual intuition for indexing versus slicing

Visual intuition for indexing versus slicing

10. Evaluate a stepped slice

For numbers = [0, 1, 2, 3, 4, 5, 6], what does numbers[::3] return? Give the exact list.

11. Trace, independent copy and nested indexing

Run this complete list program, predict the printed values, then check each variable against the trace.

Example. Trace, independent copy and nested indexing

Problem. Run this complete list program, predict the printed values, then check each variable against the trace.

Correct solution.

Complete program.```python mild_salsa = ['peppers', 'onions', 'cilantro', 'tomatoes'] hot_salsa = list(mild_salsa) hot_salsa[0] = 'hot peppers' print('mild salsa:', mild_salsa) print('hot salsa:', hot_salsa)

veg = [['lettuce', 'lettuce', 'peppers', 'zucchini'], ['lettuce', 'lettuce', 'peppers', 'zucchini'], ['lettuce', 'cilantro', 'peppers', 'zucchini']] print('row 0 basket 0:', veg[0][0]) print('row 0 basket 2:', veg[0][2]) print('last row:', veg[2])

code

Expected output.```output
mild salsa: ['peppers', 'onions', 'cilantro', 'tomatoes']
hot salsa: ['hot peppers', 'onions', 'cilantro', 'tomatoes']
row 0 basket 0: lettuce
row 0 basket 2: peppers
last row: ['lettuce', 'cilantro', 'peppers', 'zucchini']

Trace.

  1. list(mild_salsa) creates a new outer list.
  2. Changing hot_salsa[0] does not change mild_salsa.
  3. veg[0] selects the first shelf row; veg[0][0] then selects one basket value.
  4. veg[0][2] selects basket index 2 of row 0.
  5. veg[2] keeps the whole last row as a list because only the row index is used.

关键提醒。

list(...) makes an independent outer copy, and nested indexing applies the second index only to the selected row.

12. Retrieval, list syntax and independent copies

From memory, write a list literal with three filenames, then write the exact copy statement that keeps primes independent of odds = [3, 5, 7] after primes.append(2). State the two exact printed lists.

Use a bracket list literal and the list(...) copy operation.

13. Mastery, new nested list and end slice

Use the new nested input seasonal = [['winter', 12], ['spring', 20], ['summer', 31], ['autumn', 18]]. Write the two-index expressions for the summer label and summer value, and the slice expression for the last two entries. Give the exact outputs.

Index a row and then a column; slice from the end without hardcoding the length.

Practice this interactively

Free account · instant grading · spaced review that schedules itself.

Start this course for free