Lists and Iteration¶

In [ ]:
numbers = [2, 4, 6, 8]
print(numbers)
In [ ]:
names = ['Maria', 'Mario', 'Anna', 'Antonio']
print(names)

🎨 list¶

names = ['Maria', 'Mario', 'Anna', 'Antonio']

What is the value assigned to names?

In [ ]:
type(names)
number = 1
type(number)

A variable is a reference to a value.

A list is a value that contains an ordered list of references to values.

NOTES

  • Draw out the variables and the heap
    • Show a single variable pointing to a value
    • Show that a list is a value on the heap that holds references to other values.
    • Show 2, 4, 6, 8 on the heap, with arrows from the list to these values

A list serves as a collection of values.

What real-world scenarios could you represent with a list?

You can express a list using square brackets [ and ] and commas ,

In [ ]:
[1, 2, 3, 4]
In [ ]:
['dog', 'cat', 'horse']

🖌 for ... in ...¶

You can iterate over the values in a list using a for loop.

In [ ]:
numbers = [2, 4, 6, 8]
for number in numbers:
    print(number)
    
for puppy in numbers:
    print(puppy)

NOTES

  • Draw out numbers (variables and heap)
  • Draw out number - point it to the first value
  • Show how number updates with each iteration
  • Point out the indentation, just like while blocks

iteration.py¶

NOTES

  • Step through the code with a debugger
  • Show how the list appears in the variables window
  • Show how fruit updates with each pass
  • Show how we can change the name of fruit to something else

🖌 .append(...)¶

append: to put after, to place at the end

In [ ]:
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
# bigger_numbers = []
for number in numbers:
    bigger = number * 2
    bigger_numbers.append(bigger)
    print(numbers)
    print(bigger_numbers)

NOTES

  • before running the code:
    • how many times will print be called?
  • append is a function on the list, just like the Bit functions
  • append takes one argument.
    • Show what happens when there is no argument (remove the reference to bigger)

👨🏼‍🎨 smaller_numbers.py¶

This demonstrates the mapping pattern.

Map is a mathematical term that means going from one value to a corresponding value.

This is also referred to as the transform pattern.

NOTES

  • Show how smaller_numbers grows with each iteration
  • Show how the new number always goes at the end of the list
  • After walking through the code, advance the fragment and discuss the mapping pattern
  • Draw out how the numbers mapped from original to halved

👨🏾‍🎨 add_seven.py¶

You can pass lists to functions and return lists from functions.

NOTES

  • Draw out variables and heap
  • Show how numbers is updated to point to the list that new_numbers points to

🧑🏼‍🎨 only_odds.py¶

This demonstrates the filter pattern.

A new collection is created with certain items filtered out.

NOTES

  • Step through with debugger
  • Show how numbers are added only if they are odd

👨🏿‍🎨 find_min.py¶

This demonstrates the selection pattern.

A single item is selected from a collection.

NOTES

  • is None: this is how you check if a variable points to None
  • Step through in debugger
  • Look at logic of if condition
    • See how the value of smallest updates only sometimes

🎨 len¶

To get the length of a list, use len:

In [ ]:
states_visited = ['UT', 'OR', 'WA', 'ID', 'CA', 
                  'NV', 'AZ', 'WY', 'NB', 'SD', 
                  'IA', 'OH', 'NY', 'NJ', 'MA', 
                  'MD', 'VA', 'FL', 'HI', 'PA',
                  'IL', 'IN', 'MO']
print(len(states_visited))

👩🏻‍🎨 average.py¶

This demonstrates the accumulator pattern.

total accumulates the values in the collection.

NOTES

  • Step through in debugger
  • Show how total updates

Key Ideas¶

  • list
  • iteration, for ... in ...:
  • .append(...)
  • Passing lists to and returning lists from functions
  • mapping pattern
  • filter pattern
  • selection pattern
  • accumulator pattern