Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. ·...
Transcript of Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. ·...
![Page 1: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/1.jpg)
ComprehensionsCS106AP Lecture 24
![Page 2: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/2.jpg)
RoadmapProgramming Basics
The Console Images
Data structures
MidtermGraphics
Object-Oriented Programming
Everyday Python
Life after CS106AP!
Day 1!
![Page 3: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/3.jpg)
Graphics
Images
The Console
Data structures
Midterm
Programming Basics
Roadmap
Life after CS106AP!
Day 1!
Object-Oriented Programming
Everyday Python
Tuples
List Comp.
Lambdas
Internet
Computers
Life After
Final Exam
![Page 4: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/4.jpg)
Today’s questions
How can we write better loops?
What tools do we have for developing code and analyzing data?
![Page 5: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/5.jpg)
Today’s topics
1. Review
2. List and Dict Comprehensions
3. Jupyter Notebooks
4. What’s next?
![Page 6: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/6.jpg)
Review
![Page 7: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/7.jpg)
Tuples
![Page 8: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/8.jpg)
What is a tuple?
(‘a’, ‘b’, ‘c’)
(‘karel’, 1)
(‘simba’, ‘lion’, 25)
TupleAn immutable data type for storing values in an ordered
linear collection.
Definition
like a list, but written with parentheses ( ) instead of [ ]
![Page 9: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/9.jpg)
Tuples vs. Lists
● len(), print()● slicing, indexing● in● foreach loops● concatenation (creates new)● immutable
○ can’t add elements○ can’t remove elements
● len(), print()● slicing, indexing● in● foreach loops● concatenation (creates new)● mutable
○ append(), extend()○ pop(), remove()
![Page 10: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/10.jpg)
Tuples don’t support item assignment
>>> tup = (‘apple’, 0.79, ‘WA’)
>>> tup[0]
‘apple’
>>> tup[2] = ‘CA’
TypeError
You can index in to view the elements
You can’t index in to set one of the elements
![Page 11: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/11.jpg)
Tuple packing/unpacking
>>> tup = (‘apple’, 0.79, ‘WA’)
>>> tup
(‘apple’, 0.79, ‘WA’)
>>> food, price, location = tup
>>> price
0.79
unpack tuple contents into other variables
![Page 12: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/12.jpg)
Assigning multiple variables simultaneously
>>> a, b = 1, 2
>>> a
1
>>> b
2
packs 1, 2 into tuple and unpacks into variables b, a
Note: length of left-hand side must = length of right-hand side!
![Page 13: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/13.jpg)
Tuples for swapping variables
>>> a, b = 1, 2
>>> b, a = a, b
>>> b
1
>>> a
2
packs a, b into tuple and unpacks into variables b, a
![Page 14: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/14.jpg)
Tuples vs. Lists
● fixed number of elements
● know # ahead of time
● sometimes different types
● unbound number of elements
● sometimes large number
● usually all elements are the same type
not enforced by Python but good style
![Page 15: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/15.jpg)
Tuples as return values
def animal_min_feedings(animal_dict):
name, min_feedings = None, float(‘inf’)
for animal, num_feedings in animal_dict.items():
if num_feedings < min_feedings:
name, min_feedings = animal, num_feedings
return name, min_feedings
actually a tuple!
![Page 16: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/16.jpg)
Tuples as return values
def animal_min_feedings(animal_dict):
name, min_feedings = None, float(‘inf’)
for animal, num_feedings in animal_dict.items():
if num_feedings < min_feedings:
name, min_feedings = animal, num_feedings
return name, min_feedings packs name, min_feedingsinto a tuple and returns!
![Page 17: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/17.jpg)
Tuples as return values
def print_hungriest_animal(filename):
animal_dict = get_animal_feedings(filename)
animal, num = animal_min_feedings(animal_dict)
print(animal, ‘eats’, num, ‘times a day.’)
unpacks name, min_feedings tuple into two variables
![Page 18: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/18.jpg)
Sorting lists with tuples
>>> fruit = [(‘mango’, 3), (‘apple’, 6), (‘lychee’, 1), (‘apricot’, 10)]
>>> sorted(fruit)
[(‘apple’, 6), (‘apricot’, 10), (‘lychee’, 1), (‘mango’, 3)]
sorts by the first element in each tuple
![Page 19: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/19.jpg)
How can we write better loops?
![Page 20: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/20.jpg)
Problem: getting a list of squares
● Say you have a list of numbers and you want a list of those numbers’
squares.
![Page 21: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/21.jpg)
Problem: getting a list of squares
● Say you have a list of numbers and you want a list of those numbers’
squares.
[1, 3, 6, 7] --> [1, 9, 36, 49]
![Page 22: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/22.jpg)
Problem: getting a list of squares
● Say you have a list of numbers and you want a list of those numbers’
squares.
[1, 3, 6, 7] --> [1, 9, 36, 49]
● How would you produce the output list?
![Page 23: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/23.jpg)
Problem: getting a list of squares - Attempt #1
![Page 24: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/24.jpg)
Problem: getting a list of squares - Attempt #1
# [1, 3, 6, 7] --> [1, 9, 36, 49]
![Page 25: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/25.jpg)
Problem: getting a list of squares - Attempt #1
# [1, 3, 6, 7] --> [1, 9, 36, 49]def get_squared(nums): result = [] for n in nums: result.append(n ** 2) return result
![Page 26: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/26.jpg)
Problem: getting a list of squares - Attempt #1
# [1, 3, 6, 7] --> [1, 9, 36, 49]def get_squared(nums): result = [] for n in nums: result.append(n ** 2) return result
nums = [1, 3, 6, 7]squared_numbers = get_squared(nums)
![Page 27: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/27.jpg)
Problem: getting a list of squares - Attempt #2
![Page 28: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/28.jpg)
Problem: getting a list of squares - Attempt #2
# [1, 3, 6, 7] --> [1, 9, 36, 49]
![Page 29: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/29.jpg)
Problem: getting a list of squares - Attempt #2
# [1, 3, 6, 7] --> [1, 9, 36, 49]
nums = [1, 3, 6, 7]
![Page 30: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/30.jpg)
Problem: getting a list of squares - Attempt #2
# [1, 3, 6, 7] --> [1, 9, 36, 49]
nums = [1, 3, 6, 7]squared_nums = [n ** 2 for n in nums]
![Page 31: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/31.jpg)
Problem: getting a list of squares - Attempt #2
# [1, 3, 6, 7] --> [1, 9, 36, 49]
nums = [1, 3, 6, 7]squared_nums = [n ** 2 for n in nums]
this is a list comprehension!
![Page 32: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/32.jpg)
List Comprehensions
[n ** 2 for n in nums]
List ComprehensionA way to create a list
based on existing lists
Definition
![Page 33: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/33.jpg)
List Comprehensions
[n ** 2 for n in nums]
List ComprehensionA way to create a list
based on existing lists
Definitionexpression
![Page 34: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/34.jpg)
List Comprehensions
[n ** 2 for n in nums]
List ComprehensionA way to create a list
based on existing lists
Definitionexpression item
![Page 35: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/35.jpg)
List Comprehensions
[n ** 2 for n in nums]
List ComprehensionA way to create a list
based on existing lists
Definitionexpression item
list
![Page 36: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/36.jpg)
List Comprehensions
[n ** 2 for n in nums]
List ComprehensionA way to create a list
based on existing lists
Definition
![Page 37: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/37.jpg)
List Comprehensions
[n ** 2 for n in nums]
● Reuses syntax from other features:
List ComprehensionA way to create a list
based on existing lists
Definition
![Page 38: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/38.jpg)
List Comprehensions
[n ** 2 for n in nums]
● Reuses syntax from other features:○ [] to create new list
List ComprehensionA way to create a list
based on existing lists
Definition
![Page 39: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/39.jpg)
List Comprehensions
[n ** 2 for n in nums]
● Reuses syntax from other features:○ [] to create new list○ foreach loop over other list
List ComprehensionA way to create a list
based on existing lists
Definition
![Page 40: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/40.jpg)
Problem: getting a list of lowercase strings
![Page 41: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/41.jpg)
Problem: getting a list of lowercase strings
● Say you have a list of strings with random casing and you want them to be lowercase.
![Page 42: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/42.jpg)
Problem: getting a list of lowercase strings
● Say you have a list of strings with random casing and you want them to be lowercase.
in: [‘SOnja’, ‘nicHOLAs’, ‘KYLiE’]
out: [‘sonja’, ‘nicholas’, ‘kylie’]
![Page 43: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/43.jpg)
Problem: getting a list of lowercase strings
● Say you have a list of strings with random casing and you want them to be lowercase.
in: [‘SOnja’, ‘nicHOLAs’, ‘KYLiE’]
out: [‘sonja’, ‘nicholas’, ‘kylie’]
● How would you produce the output list?
![Page 44: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/44.jpg)
Problem: getting a list of lowercase strings
![Page 45: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/45.jpg)
Problem: getting a list of lowercase strings
strings = [‘SOnja’, ‘nicHOLAs’, ‘KYLiE’]
![Page 46: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/46.jpg)
Problem: getting a list of lowercase strings
strings = [‘SOnja’, ‘nicHOLAs’, ‘KYLiE’]
lower_strings = [s.lower() for s in strings]
![Page 47: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/47.jpg)
Problem: getting a list of lowercase strings
strings = [‘SOnja’, ‘nicHOLAs’, ‘KYLiE’]
lower_strings = [s.lower() for s in strings]
expression item list
![Page 48: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/48.jpg)
Problem: getting a list of lowercase strings
strings = [‘SOnja’, ‘nicHOLAs’, ‘KYLiE’]
lower_strings = [s.lower() for s in strings]
Note: a list comprehension creates a new list and doesn’t modify the old list
expression item list
![Page 49: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/49.jpg)
Problem: getting a list of formatted names
![Page 50: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/50.jpg)
Problem: getting a list of formatted names
● Say you have a list of strings with random casing and you want them to have the first letter uppercase and other letters lowercase.
![Page 51: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/51.jpg)
Problem: getting a list of formatted names
● Say you have a list of strings with random casing and you want them to have the first letter uppercase and other letters lowercase.
in: [‘SOnja’, ‘nicHOLAs’, ‘KYLiE’]
out: [‘Sonja’, ‘Nicholas’, ‘Kylie’]
![Page 52: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/52.jpg)
Problem: getting a list of formatted names
● Say you have a list of strings with random casing and you want them to have the first letter uppercase and other letters lowercase.
in: [‘SOnja’, ‘nicHOLAs’, ‘KYLiE’]
out: [‘Sonja’, ‘Nicholas’, ‘Kylie’]
Think/Pair/Share:How would you produce the
output list?
![Page 53: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/53.jpg)
Problem: getting a list of formatted names
![Page 54: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/54.jpg)
Problem: getting a list of formatted names
strings = [‘SOnja’, ‘nicHOLAs’, ‘KYLiE’]
![Page 55: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/55.jpg)
Problem: getting a list of formatted names
strings = [‘SOnja’, ‘nicHOLAs’, ‘KYLiE’]
name_strings = [s[0].upper() + s[1:].lower() for s in strings]
![Page 56: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/56.jpg)
Problem: getting a list of formatted names
strings = [‘SOnja’, ‘nicHOLAs’, ‘KYLiE’]
name_strings = [s[0].upper() + s[1:].lower() for s in strings]
expression
![Page 57: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/57.jpg)
Problem: getting a list of formatted names
strings = [‘SOnja’, ‘nicHOLAs’, ‘KYLiE’]
name_strings = [s[0].upper() + s[1:].lower() for s in strings]
expression item
![Page 58: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/58.jpg)
Problem: getting a list of formatted names
strings = [‘SOnja’, ‘nicHOLAs’, ‘KYLiE’]
name_strings = [s[0].upper() + s[1:].lower() for s in strings]
expression item list
![Page 59: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/59.jpg)
Combining functions with list comprehensions
strings = [‘SOnja’, ‘nicHOLAs’, ‘KYLiE’]
name_strings = [s[0].upper() + s[1:].lower() for s in strings]
expression can be decomposed out into a function!
![Page 60: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/60.jpg)
Combining functions with list comprehensions
def name_case(s):
return s[0].upper() + s[1:].lower()
![Page 61: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/61.jpg)
Combining functions with list comprehensions
def name_case(s):
return s[0].upper() + s[1:].lower()
strings = [‘SOnja’, ‘nicHOLAs’, ‘KYLiE’]
![Page 62: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/62.jpg)
Combining functions with list comprehensions
def name_case(s):
return s[0].upper() + s[1:].lower()
strings = [‘SOnja’, ‘nicHOLAs’, ‘KYLiE’]
name_strings = [name_case(s) for s in strings]
![Page 63: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/63.jpg)
Problem: getting a list of tuples
● Say you have a list of strings. Return a list of tuples where the first elem is the string and the second elem is the length of the string.
![Page 64: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/64.jpg)
Problem: getting a list of tuples
● Say you have a list of strings. Return a list of tuples where the first elem is the string and the second elem is the length of the string.
in: [‘I’, ‘love’, ‘CS106AP’]
out: [(‘I’, 1), (‘love’, 4), (‘CS106AP’, 7)]
![Page 65: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/65.jpg)
Problem: getting a list of tuples
● Say you have a list of strings. Return a list of tuples where the first elem is the string and the second elem is the length of the string.
in: [‘I’, ‘love’, ‘CS106AP’]
out: [(‘I’, 1), (‘love’, 4), (‘CS106AP’, 7)]
Think/Pair/Share:How would you produce the
output list?
![Page 66: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/66.jpg)
Problem: getting a list of tuples
![Page 67: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/67.jpg)
Problem: getting a list of tuples
strings = [‘I’, ‘love’, ‘CS106AP’]
![Page 68: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/68.jpg)
Problem: getting a list of tuples
strings = [‘I’, ‘love’, ‘CS106AP’]
tuples = [(s, len(s)) for s in strings]
![Page 69: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/69.jpg)
Problem: getting a list of tuples
strings = [‘I’, ‘love’, ‘CS106AP’]
tuples = [(s, len(s)) for s in strings]
expression
![Page 70: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/70.jpg)
Problem: getting a list of tuples
strings = [‘I’, ‘love’, ‘CS106AP’]
tuples = [(s, len(s)) for s in strings]
expression item
![Page 71: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/71.jpg)
Problem: getting a list of tuples
strings = [‘I’, ‘love’, ‘CS106AP’]
tuples = [(s, len(s)) for s in strings]
expression item list
![Page 72: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/72.jpg)
Conditions in list comprehensions
>>> nums = [4, 23, 9, 18, 63, 42]
![Page 73: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/73.jpg)
Conditions in list comprehensions
>>> nums = [4, 23, 9, 18, 63, 42]what if I only want even numbers?
![Page 74: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/74.jpg)
Conditions in list comprehensions
>>> nums = [4, 23, 9, 18, 63, 42]
>>> even_nums = [n for n in nums if n % 2 == 0]
![Page 75: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/75.jpg)
Conditions in list comprehensions
>>> nums = [4, 23, 9, 18, 63, 42]
>>> even_nums = [n for n in nums if n % 2 == 0]
>>> even_nums
![Page 76: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/76.jpg)
Conditions in list comprehensions
>>> nums = [4, 23, 9, 18, 63, 42]
>>> even_nums = [n for n in nums if n % 2 == 0]
>>> even_nums
[4, 18, 42]
![Page 77: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/77.jpg)
Conditions in list comprehensions
● You can add a condition for additional “filtering”
![Page 78: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/78.jpg)
Conditions in list comprehensions
● You can add a condition for additional “filtering”
[expression for item in list if condition]
![Page 79: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/79.jpg)
Conditions in list comprehensions
● You can add a condition for additional “filtering”
[expression for item in list if condition]
[n for n in nums if n % 2 == 0]
![Page 80: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/80.jpg)
Conditions in list comprehensions
● You can add a condition for additional “filtering”
[expression for item in list if condition]
[n for n in nums if n % 2 == 0]
expression
![Page 81: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/81.jpg)
Conditions in list comprehensions
● You can add a condition for additional “filtering”
[expression for item in list if condition]
[n for n in nums if n % 2 == 0]
expression item
![Page 82: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/82.jpg)
Conditions in list comprehensions
● You can add a condition for additional “filtering”
[expression for item in list if condition]
[n for n in nums if n % 2 == 0]
expression item list
![Page 83: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/83.jpg)
Conditions in list comprehensions
● You can add a condition for additional “filtering”
[expression for item in list if condition]
[n for n in nums if n % 2 == 0]
expression item list condition
![Page 84: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/84.jpg)
Conditions in list comprehensions
>>> strings = [‘sonja’, ‘kylie’, ‘nick’]
![Page 85: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/85.jpg)
Conditions in list comprehensions
>>> strings = [‘sonja’, ‘kylie’, ‘nick’]
what if I don’t want strings that start with ‘s’?
![Page 86: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/86.jpg)
Conditions in list comprehensions
>>> strings = [‘sonja’, ‘kylie’, ‘nick’]
>>> no_s_strings = [s for s in strings if s[0].lower() != ‘s’]
![Page 87: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/87.jpg)
Conditions in list comprehensions
>>> strings = [‘sonja’, ‘kylie’, ‘nick’]
>>> no_s_strings = [s for s in strings if s[0].lower() != ‘s’]
>>> no_s_strings
![Page 88: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/88.jpg)
Conditions in list comprehensions
>>> strings = [‘sonja’, ‘kylie’, ‘nick’]
>>> no_s_strings = [s for s in strings if s[0].lower() != ‘s’]
>>> no_s_strings
[‘kylie’, ‘nick’]
![Page 89: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/89.jpg)
Conditions in list comprehensions
>>> song = [‘the’, ‘wheels’, ‘on’, ‘the’, ‘bus, ‘go’,‘round’, ‘and’, ‘round’]
![Page 90: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/90.jpg)
Conditions in list comprehensions
>>> song = [‘the’, ‘wheels’, ‘on’, ‘the’, ‘bus, ‘go’,‘round’, ‘and’, ‘round’]
>>> round_words = [‘wheels’, ‘round’, ‘circle’]
![Page 91: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/91.jpg)
Conditions in list comprehensions
>>> song = [‘the’, ‘wheels’, ‘on’, ‘the’, ‘bus, ‘go’,‘round’, ‘and’, ‘round’]
>>> round_words = [‘wheels’, ‘round’, ‘circle’]
>>> not_round_song = [word for word in song if word not in round_words]
![Page 92: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/92.jpg)
Conditions in list comprehensions
>>> song = [‘the’, ‘wheels’, ‘on’, ‘the’, ‘bus, ‘go’,‘round’, ‘and’, ‘round’]
>>> round_words = [‘wheels’, ‘round’, ‘circle’]
>>> not_round_song = [word for word in song if word not in round_words]
>>> not_round_song
![Page 93: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/93.jpg)
Conditions in list comprehensions
>>> song = [‘the’, ‘wheels’, ‘on’, ‘the’, ‘bus, ‘go’,‘round’, ‘and’, ‘round’]
>>> round_words = [‘wheels’, ‘round’, ‘circle’]
>>> not_round_song = [word for word in song if word not in round_words]
>>> not_round_song
[‘the’, ‘on’, ‘the’, ‘bus, ‘go’,‘and’]
![Page 94: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/94.jpg)
Why list comprehensions?
![Page 95: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/95.jpg)
Why list comprehensions?
● They’re more concise
![Page 96: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/96.jpg)
Why list comprehensions?
● They’re more concise
● They’re faster
![Page 97: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/97.jpg)
Why list comprehensions?
● They’re more concise
● They’re faster
● They’re Pythonic
![Page 98: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/98.jpg)
When to not use list comprehensions
![Page 99: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/99.jpg)
When to not use list comprehensions
● When you need more than one condition
![Page 100: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/100.jpg)
When to not use list comprehensions
● When you need more than one condition
● When the expression is complex
![Page 101: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/101.jpg)
When to not use list comprehensions
● When you need more than one condition
● When the expression is complex
○ Break it out into a separate function!
![Page 102: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/102.jpg)
Dictionary Comprehensions
![Page 103: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/103.jpg)
Problem: updating all the values in a dict
● Say you have a dictionary of strings to ints, and you want to double the values in the dictionary.
![Page 104: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/104.jpg)
Problem: updating all the values in a dict
● Say you have a dictionary of strings to ints, and you want to double the values in the dictionary.
in: {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
out: {‘a’: 2, ‘b’: 4, ‘c’: 6, ‘d’: 8}
![Page 105: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/105.jpg)
Problem: updating all the values in a dict
● Say you have a dictionary of strings to ints, and you want to double the values in the dictionary.
in: {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
out: {‘a’: 2, ‘b’: 4, ‘c’: 6, ‘d’: 8}
● How would you produce the output dictionary?
![Page 106: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/106.jpg)
Dict Comprehensions
d = {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
![Page 107: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/107.jpg)
Dict Comprehensions
d = {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
doubled = {k:2*v for (k, v) in d.items()}
![Page 108: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/108.jpg)
Dict Comprehensions
d = {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
doubled = {k:2*v for (k, v) in d.items()}
new key
![Page 109: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/109.jpg)
Dict Comprehensions
d = {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
doubled = {k:2*v for (k, v) in d.items()}
new key new value
![Page 110: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/110.jpg)
Dict Comprehensions
d = {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
doubled = {k:2*v for (k, v) in d.items()}
new key itemnew value
![Page 111: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/111.jpg)
Dict Comprehensions
d = {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
doubled = {k:2*v for (k, v) in d.items()}
new key item iterablenew value
![Page 112: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/112.jpg)
Problem: reversing the keys and values in a dict
● Make the keys the values and the values the keys!
in: {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
out: {1: ‘a’, 2: ‘b’, 3: ‘c’, 4: ‘d’}
Think/Pair/Share:How would you produce the
output dictionary?
![Page 113: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/113.jpg)
Problem: reversing the keys and values in a dict
● Make the keys the values and the values the keys!
in: {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
out: {1: ‘a’, 2: ‘b’, 3: ‘c’, 4: ‘d’}
Think/Pair/Share:How would you produce the
output dictionary?
doubled = {k:2*v for (k, v) in d.items()}
![Page 114: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/114.jpg)
Dict Comprehensions
d = {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
![Page 115: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/115.jpg)
Dict Comprehensions
d = {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
flipped = {v:k for (k, v) in d.items()}
![Page 116: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/116.jpg)
Dict Comprehensions
d = {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
flipped = {v:k for (k, v) in d.items()}
new key
![Page 117: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/117.jpg)
Dict Comprehensions
d = {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
flipped = {v:k for (k, v) in d.items()}
new key new value
![Page 118: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/118.jpg)
Dict Comprehensions
d = {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
flipped = {v:k for (k, v) in d.items()}
new key itemnew value
![Page 119: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/119.jpg)
Dict Comprehensions
d = {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
flipped = {v:k for (k, v) in d.items()}
new key item iterablenew value
![Page 120: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/120.jpg)
What tools do we have for developing code and analyzing
data?
![Page 121: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/121.jpg)
Jupyter Notebooks
![Page 122: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/122.jpg)
Jupyter Notebook
● Interactive “notebook” where you can run parts of code
![Page 123: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/123.jpg)
Jupyter Notebook
● Interactive “notebook” where you can run parts of code
○ Can develop code step-by-step
![Page 124: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/124.jpg)
Jupyter Notebook
● Interactive “notebook” where you can run parts of code
○ Can develop code step-by-step
○ Great for data analysis
![Page 125: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/125.jpg)
Jupyter Notebook
● Interactive “notebook” where you can run parts of code
○ Can develop code step-by-step
○ Great for data analysis
● Built on top of regular Python code
![Page 126: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/126.jpg)
Jupyter Notebook Setup
$ python3 -m pip install jupyter
![Page 127: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/127.jpg)
Jupyter Notebook: Investigating California Air Quality
![Page 128: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/128.jpg)
What’s next?
![Page 129: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/129.jpg)
Graphics
Images
The Console
Data structures
Midterm
Programming Basics
Roadmap
Life after CS106AP!
Day 1!
Object-Oriented Programming
Everyday Python
Tuples
List Comp.
Lambdas
Internet
Computers
Life After
Final Exam
![Page 130: Comprehensionsweb.stanford.edu/.../24-Comprehensions/24-Comprehensions.pdf · 2019. 8. 6. · Everyday Python Life after CS106AP! Day 1! Graphics The Console Images Data structures](https://reader033.fdocuments.us/reader033/viewer/2022052016/602e103fa8d29b058d2178c8/html5/thumbnails/130.jpg)
Lambdas and Custom Sort
● How can we customize our sorting?