You are on page 1of 153

Data Structure - Lists

Adopted from Stanford Uni’s CS106ap course slides by Kylie Jue and Sonja Johnson-Yu
How can we store and organize
Today’s data in our code?

questions
1. Review


Today’s 
 2. Introduction to Lists


topics 3. Advanced Lists


4. What’s next?
Review
Console programs
Definition
Console program
A program that solicits input from a user via
an interactive terminal (console) and does
something interesting with that input
Console program summary

● Use input(prompt) to read in information from the user.


○ Make sure to convert the data to the correct type (from the
string data type)!


● Use print() to display information for the user.


○ Make sure to convert the data to the correct type (from the int/
float data type)


● Use a while loop to enable multiple runs of your program.


Strings
Definition
string
A data type that represents a sequence of
characters
Characters can
be letters, digits,
Definition symbols (&, !, ~),
string
etc.
A data type that represents a sequence of
characters
String fundamentals

● String literals are any string of characters enclosed in single (‘’) or


double quotes (“”)


● Each character in the string is associated with an index


● Strings can be combined with the + operator in a process called


concatenation


● Strings are immutable


Indexing and slicing

● Length
○ The length of a string is the number of characters it contains
○ We can use the Python function len() to evaluate the length of a
string
len(‘banana’) → 6
len(‘’) → 0
len(‘CS106AP rocks my socks’) → 22
Slides courtesy of Sam Redmond, CS41
Slides courtesy of Sam Redmond, CS41
What are the
correct indices?
Slides courtesy of Sam Redmond, CS41
Slides courtesy of Sam Redmond, CS41
Slides courtesy of Sam Redmond, CS41
String functions

● All follow the noun.verb() syntax we’ve seen before


● str.isupper(), str.islower()

● str.isalpha(), str.isdigit()

● str.upper(), str.lower()
String functions

● All follow the noun.verb() syntax we’ve seen before


● str.isupper(), str.islower()
Return True or
● str.isalpha(), str.isdigit() False
● str.upper(), str.lower()
String functions

● All follow the noun.verb() syntax we’ve seen before


● str.isupper(), str.islower()

● str.isalpha(), str.isdigit()

● str.upper(), str.lower() Return updated


string
String functions

● All follow the noun.verb() syntax we’ve seen before


● str.isupper(), str.islower()

● str.isalpha(), str.isdigit()

● str.upper(), str.lower() Return updated


string
Remember: Original string is unchanged
because of immutability!
Type conversion

● Important note: ‘123’ is a string and 123 is an int

● In order to convert between data types, we can use built-in Python


functions: str(), int(), float()

int(‘123’) == 123
float(‘24.7’) == 24.7
str(12345) == ‘12345’
str(20.19) == ‘20.19’
Doctests
Doctests

● Python has a great testing framework called doctests


○ For each function in your program, write doctests that specify an
output for a given input
○ You can (and should) have multiple doctests per function
Doctests

● Python has a great testing framework called doctests


● PyCharm supports doctests by allowing you to easily run them in the


editor
○ Put doctests in function header comments using `>>>`
Doctests
● Python has a great testing framework called doctests


● PyCharm supports doctests by allowing you to easily run them in the editor
○ Put doctests in function header comments using `>>>`
def add(a, b):
“““
>>> add(2, 4)
6
”””
...
Doctests
● Python has a great testing framework called doctests


● PyCharm supports doctests by allowing you to easily run them in the editor
○ Put doctests in function header comments using `>>>`
def add(a, b):
“““
Call the function
>>> add(2, 4)
6
and specify any
””” arguments if
...
needed.
Doctests
● Python has a great testing framework called doctests


● PyCharm supports doctests by allowing you to easily run them in the editor
○ Put doctests in function header comments using `>>>`
def add(a, b):
“““
>>> add(2, 4)
6 Put the expected
”””
...
output directly
after the test.
Testing strategies

● Write tests that cover a wide variety of use cases for your function!


● Consider:
○ Basic use cases
○ Edge cases
Definition
edge case
Uses of your function/program that
represent extreme situations
EliminationNation.py
[demo]
Takeaways

● Common pattern: processing all characters in a string


for i in range(len(s)):
current_char = s[i]
# Use current_char
Takeaways

● Common pattern: processing all characters in a string


● Common pattern: building up a new string


new_string = ‘’
for i in range(len(s)):
new_string += s[i]
Takeaways

● Common pattern: processing all characters in a string


● Common pattern: building up a new string


new_string = ‘’
for i in range(len(s)):
if __________:
new_string += s[i]
Takeaways

● Common pattern: processing all characters in a string


● Common pattern: building up a new string


Select only certain
characters - think
new_string = ‘’ of this as a
for i in range(len(s)):
if __________: filter!
new_string += s[i]
Takeaways

● Common pattern: processing all characters in a string


● Common pattern: building up a new string


● Write doctests for every function!


○ Cover a range of usage patterns for your function
○ Write them before writing the actual function code
○ Run them often as you make changes
Takeaways

● Common pattern: processing all characters in a string


● Common pattern: building up a new string


● Write doctests for every function!


○ Cover a range of usage patterns for your function
○ Write them before writing the actual function code
○ Run them often as you make changes
How can we store and organize data in code?
Lists!
Sequences
• Sequence: an object that contains
multiple items of data
• The items are stored in sequence one after
another
• Python provides different types of
sequences, including lists and tuples
• The difference between these is that a list is
mutable and a tuple is immutable

Copyright © 2018 Pearson Education, Ltd.


What is a list? Definition

List
A data type for storing
values in a linear collection.
What is a list? Definition
[1, 2, 3, 4, 5]

List
A data type for storing
values in a linear collection.
What is a list? Definition
[1, 2, 3, 4, 5]

List
A data type for storing
Use [ ] to values in a linear collection.
write a list
in code!
What is a list? Definition
[1, 2, 3, 4, 5]

List
A data type for storing
Lists contain values in a linear collection.
elements!
(separate with
commas)
What is a list? Definition
[1, 2, 3, 4, 5]

[‘a’, ‘b’, ‘b’, ‘d’] List


A data type for storing
values in a linear collection.
What is a list? Definition
[1, 2, 3, 4, 5]

[‘a’, ‘b’, ‘b’, ‘d’] List


[True]
A data type for storing
values in a linear collection.
What is a list? Definition
[1, 2, 3, 4, 5]

[‘a’, ‘b’, ‘b’, ‘d’] List


[True]
A data type for storing
values in a linear collection.

Lists can
have 1
element!
What is a list? Definition
[1, 2, 3, 4, 5]

[‘a’, ‘b’, ‘b’, ‘d’] List


[True]
A data type for storing
values in a linear collection.
[1, ‘a’, 2, ‘b’, True]
What is a list? Definition
[1, 2, 3, 4, 5]

[‘a’, ‘b’, ‘b’, ‘d’] List


[True]
A data type for storing
values in a linear collection.
[1, ‘a’, 2, ‘b’, True]

Lists can contain different


types!
What is a list? Definition
[1, 2, 3, 4, 5]

[‘a’, ‘b’, ‘b’, ‘d’] List


[True]
A data type for storing
values in a linear collection.
[1, ‘a’, 2, ‘b’, True]

[]
What is a list? Definition
[1, 2, 3, 4, 5]

[‘a’, ‘b’, ‘b’, ‘d’] List


[True]
A data type for storing
values in a linear collection.
[1, ‘a’, 2, ‘b’, True]

[]
compare to the empty string
(‘’)!
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]

>>> letters[0]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]

>>> letters[0]

‘a’
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]

>>> letters[0]

‘a’

>>> letters[3]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]

>>> letters[0]

‘a’

>>> letters[3]

‘d’
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]

>>> letters[0]

‘a’ Indexing lists is similar to


>>> letters[3] indexing strings, except with
‘d’ elements instead of
characters!
The len function
• An IndexError exception is raised if an invalid
index is used
• len function: returns the length of a sequence
such as a list
• Example: size = len(my_list)
• Returns the number of elements in the list, so the
index of last element is len(list)-1
• Can be used to prevent an IndexError exception
when iterating over a list with a loop

Copyright © 2018 Pearson Education, Ltd.


How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]

>>> len(letters)

4
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]

>>> len(letters)

4 len() works for lists as well!


List Slicing
• Slice: a span of items that are taken from a
sequence
• List slicing format: list[start : end]
• Span is a list containing copies of elements from
start up to, but not including, end
• If start not specified, 0 is used for start index
• If end not specified, len(list) is used for end index
• Slicing expressions can include a step value and
negative indexes relative to end of list

Copyright © 2018 Pearson Education, Ltd.


How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]

>>> letters[:2]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]

>>> letters[:2]

[‘a’, ‘b’]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]

>>> letters[:2]

[‘a’, ‘b’]

>>> letters[1:]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]

>>> letters[:2]

[‘a’, ‘b’]

>>> letters[1:]

[‘b’, ‘c’, ‘d’]


How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]

>>> letters[:2]

[‘a’, ‘b’] Slicing is similar to strings,


>>> letters[1:] too!
[‘b’, ‘c’, ‘d’]
Printing lists
>>> fruits = [‘apple’, ‘banana’, ‘mango’]
Printing lists
>>> fruits = [‘apple’, ‘banana’, ‘mango’]

>>> print(fruits)
Printing lists
>>> fruits = [‘apple’, ‘banana’, ‘mango’]

>>> print(fruits)

[‘apple’, ‘banana’, ‘mango’]


Lists Are Mutable
• Mutable sequence: the items in the sequence
can be changed
• Lists are mutable, and so their elements can be
changed
• An expression such as
• list[1] = new_value can be used to
assign a new value to a list element
• Must use a valid index to prevent raising of an
IndexError exception

Copyright © 2018 Pearson Education, Ltd.


Concatenating Lists
• Concatenate: join two things together
• The + operator can be used to
concatenate two lists
– Cannot concatenate a list with another data
type, such as a number
• The += augmented assignment operator
can also be used to concatenate lists

Copyright © 2018 Pearson Education, Ltd.


Repetition Operator
• The * operator makes multiple copies of
a list and joins them all together.
– List * n

Copyright © 2018 Pearson Education, Ltd.


How can I make multiples copies of a list
>>> numbers = [0] * 5

>>> numbers

[0, 0, 0, 0, 0]
>>> numbers = [1, 2, 3] * 3

>>> numbers

[1, 2, 3, 1, 2, 3, 1, 2, 3]
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]

>>> lst += [6, 7]


How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]

>>> lst += [6, 7]

>>> lst
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]

>>> lst += [6, 7]

>>> lst

[1, 2, 3, 4, 5, 6, 7]
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]

>>> lst += [6, 7]

>>> lst

[1, 2, 3, 4, 5, 6, 7]

>>> lst += 8
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]

>>> lst += [6, 7]

>>> lst

[1, 2, 3, 4, 5, 6, 7]

>>> lst += TypeError


8
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]

>>> lst += [6, 7]

>>> lst

[1, 2, 3, 4, 5, 6, 7]

>>> lst += 8
you can only use += for
concatenating other lists!
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]

>>> lst.append(6)
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]

>>> lst.append(6)

append adds a single


element to the end of a
list!
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]

>>> lst.append(6)

>>> lst
append adds a single
element to the end of a
list!
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]

>>> lst.append(6)

>>> lst
append adds a single
[1, 2, 3, 4, 5, 6]
element to the end of a
list!
How can I remove something from a list?
>>> lst = [1, 2, 3, 4, 5]
How can I remove something from a list?
>>> lst = [1, 2, 3, 4, 5]

>>> last_elem = lst.pop()


How can I remove something from a list?
>>> lst = [1, 2, 3, 4, 5]

>>> last_elem = lst.pop()

pop() removes the last


element in a list and
returns it
How can I remove something from a list?
>>> lst = [1, 2, 3, 4, 5]

>>> last_elem = lst.pop()

>>> last_elem
pop() removes the last
element in a list and
returns it
How can I remove something from a list?
>>> lst = [1, 2, 3, 4, 5]

>>> last_elem = lst.pop()

>>> last_elem
pop() removes the last
5
element in a list and
returns it
How can I remove something from a list?
>>> lst = [1, 2, 3, 4, 5]

>>> last_elem = lst.pop()

>>> last_elem
pop() removes the last
5
element in a list and
>>> lst
returns it
How can I remove something from a list?
>>> lst = [1, 2, 3, 4, 5]

>>> last_elem = lst.pop()

>>> last_elem
pop() removes the last
5
element in a list and
>>> lst
returns it
[1, 2, 3, 4]
How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]
How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]

>>> ‘mango’ in fruits


How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]

>>> ‘mango’ in fruits returns True if element in


list,
else False!
How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]

>>> ‘mango’ in fruits returns True if element in


True list,
else False!
How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]

>>> ‘mango’ in fruits returns True if element in


True list,
>>> ‘broccoli’ in fruits else False!
How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]

>>> ‘mango’ in fruits returns True if element in


True list,
>>> ‘broccoli’ in fruits else False!
False
How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]

>>> ‘mango’ in fruits returns True if element in


True list,
>>> ‘broccoli’ in fruits else False!
False

>>> ‘broccoli’ not in fruits


How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]

>>> ‘mango’ in fruits returns True if element in


True list,
>>> ‘broccoli’ in fruits else False!
False
you can use not with in
>>> ‘broccoli’ not in fruits
How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]

>>> ‘mango’ in fruits


returns True if element in
True list,
>>> ‘broccoli’ in fruits else False!
False

>>> ‘broccoli’ not in fruits you can use not with in


True
Finding Items in Lists with the in
Operator
• You can use the in operator to determine
whether an item is contained in a list
• General format: item in list
• Returns True if the item is in the list, or False if it is
not in the list
• Similarly you can use the not in operator to
determine whether an item is not in a list

Copyright © 2018 Pearson Education, Ltd.


How can I loop over a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’]
How can I loop over a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’]

>>> for fruit in fruits:


How can I loop over a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’]

>>> for fruit in fruits:


How can I loop over a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’]

>>> for fruit in fruits:


like a foreach loop over a
string!
How can I loop over a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’]

>>> for fruit in fruits:


like a foreach loop over a
... print(fruit) string!
How can I loop over a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’]

>>> for fruit in fruits:


like a foreach loop over a
... print(fruit) string!
apple

banana

mango
How can I loop over a list?
>>> find_min([2.3, 7.1, 10.6])

2.3
Think/Pair/Share:
# syntax reminder: 

Write a function
for elem in lst: find_min()that returns
the minimum float in a
# do something list.
Think/Pair/Share:

Write a function find_min() that


returns the minimum float in a list.
Making a list from a string
>>> s = ‘I am comprised of words’
Making a list from a string
>>> s = ‘I am comprised of words’

>>> words = s.split()


Making a list from a string
>>> s = ‘I am comprised of words’

>>> words = s.split()


splits string where there are
spaces
Making a list from a string
>>> s = ‘I am comprised of words’

>>> words = s.split()


splits string where there are
>>> words spaces
Making a list from a string
>>> s = ‘I am comprised of words’

>>> words = s.split()


splits string where there are
>>> words spaces
[‘I’, ‘am’, ‘comprised’, ‘of’, ‘words’]
Making a list from a string
>>> s = ‘I am comprised of words’

>>> words = s.split()


splits string where there are
>>> words spaces
[‘I’, ‘am’, ‘comprised’, ‘of’, ‘words’]

>>> s
Making a list from a string
>>> s = ‘I am comprised of words’

>>> words = s.split()


splits string where there are
>>> words spaces
[‘I’, ‘am’, ‘comprised’, ‘of’, ‘words’]

>>> s

‘I am comprised of words’
Making a list from a string
>>> s = ‘do,re,mi,fa,sol,la,ti’
Making a list from a string
>>> s = ‘do,re,mi,fa,sol,la,ti’

>>> notes = s.split(‘,’)


Making a list from a string
>>> s = ‘do,re,mi,fa,sol,la,ti’

>>> notes = s.split(‘,’)


pass in a “delimiter”, which
tells us
where to split the string
Making a list from a string
>>> s = ‘do,re,mi,fa,sol,la,ti’

>>> notes = s.split(‘,’)


pass in a “delimiter”, which
>>> notes tells us
where to split the string
Making a list from a string
>>> s = ‘do,re,mi,fa,sol,la,ti’

>>> notes = s.split(‘,’)


pass in a “delimiter”, which tells us
>>> notes where to split the string
[‘do’, ‘re’, ‘mi’, ‘fa’, ‘sol’, ‘la’, ‘ti’]
Making a list from a range
>>> numbers = list (range(5))

[0, 1, 2, 3, 4]
>>> numbers = list (range(1, 10, 2))

[0, 3, 4, 7, 9]
Think/Pair/Share:

Write words_starting_with()
Inputs:
sentence (string)
char (string)
Returns:
list of words starting with character
Making a list from a string

>>> words_starting_with('I love ice cream', 'i')


Think/Pair/Share:
['I', 'ice']

Write a function
# syntax reminder: words_starting_with().
lst = s.split() Inputs:
sentence (string)
char (string)
Returns:
list of words starting with
character
List Methods and Useful Built-in
Functions
• append(item): used to add items to a list –
item is appended to the end of the existing list
• index(item): used to determine where an item
is located in a list
• Returns the index of the first element in the list
containing item
• Raises ValueError exception if item not in the list

Copyright © 2018 Pearson Education, Ltd.


List Methods and Useful Built-in
Functions (cont’d.)
• insert(index, item): used to insert
item at position index in the list
• sort(): used to sort the elements of
the list in ascending order
• remove(item): removes the first
occurrence of item in the list
• reverse(): reverses the order of the
elements in the list

Copyright © 2018 Pearson Education, Ltd.


Copyright © 2018 Pearson Education, Ltd.
List Methods and Useful Built-in
Functions (cont’d.)
• del statement: removes an element from a
specific index in a list
• General format: del list[i]
• min and max functions: built-in functions that
returns the item that has the lowest or highest
value in a sequence
• The sequence is passed as an argument

Copyright © 2018 Pearson Education, Ltd.


Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::-1]
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::-1]

[‘e’, ‘d’, ‘c’, ‘b’, ‘a’]


Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::-1]

[‘e’, ‘d’, ‘c’, ‘b’, ‘a’]


Think/Pair/Share:

What just happened? What is
going on in the [::-1]?
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::-1]
(start_index, end_index,
[‘e’, ‘d’, ‘c’, ‘b’, ‘a’] step)
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::-1]
(start_index, end_index,
[‘e’, ‘d’, ‘c’, ‘b’, ‘a’] step)

for i in range(4, 0, -1):


Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::-1]
(start_index, end_index,
[‘e’, ‘d’, ‘c’, ‘b’, ‘a’] step)
>>> lst
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::-1]
(start_index, end_index,
[‘e’, ‘d’, ‘c’, ‘b’, ‘a’] step)
>>> lst

[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]


Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::2]
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::2]

Think/Pair/Share:

What will this return?
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::2]

[‘a’, ‘c’, ‘e’]


Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::2]
(start_index, end_index,
[‘a’, ‘c’, ‘e’] step)
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::2]
(start_index, end_index,
[‘a’, ‘c’, ‘e’] step)

for i in range(0, 6, 2):


Copying Lists
• To make a copy of a list you must copy
each element of the list
• Two methods to do this:
• Creating a new empty list and using a for loop
to add a copy of each element from the original
list to the new list
• Creating a new empty list and concatenating the
old list to the new empty list

Copyright © 2018 Pearson Education, Ltd.


Copying Lists (cont’d.)

Copyright © 2018 Pearson Education, Ltd.


Processing Lists
• List elements can be used in calculations
• To calculate total of numeric values in a list use
loop with accumulator variable
• To average numeric values in a list:
• Calculate total of the values
• Divide total of the values by len(list)
• List can be passed as an argument to a function
• List can be returned from a function

Copyright © 2018 Pearson Education, Ltd.


Think/Pair/Share:

Get a list of grades from a user and


find the average of them by dropping
the lowest grade
Two-Dimensional Lists
• Two-dimensional list: a list that contains other
lists as its elements
• Also known as nested list
• Common to think of two-dimensional lists as having
rows and columns
• Useful for working with multiple sets of data
• To process data in a two-dimensional list need
to use two indexes
• Typically use nested loops to process

Copyright © 2018 Pearson Education, Ltd.


Two-Dimensional Lists (cont’d.)

Copyright © 2018 Pearson Education, Ltd.


Two-Dimensional Lists (cont’d.)

Copyright © 2018 Pearson Education, Ltd.


Think/Pair/Share:

Write two-dimensional array with


random numbers.
What’s next?
How do computers store and
Next Lecture’s organize data?

questions How can I give instructions to my


computer outside of a program?

You might also like