August 2nd, 2021
Today: list comprehensions, another way to plot data
Guiding question: how can you make a list without using map or a for loop?
# strategy 1: use a for loop to build up a new list def get_squared(num_lst): squares = [] for num in num_lst: squares.append(num**2) return squares
# strategy 2: use map with a lambda function num_list = [4, 6, 7, 8] squares = map(lambda n: n**2, num_list)
Here is a third strategy. Checkout the example below, then we will walk through what it meants
# strategy 3: List Comprehension num_list = [4, 6, 7, 8] squares = [n ** 2 for n in num_list]
This is a list comprehension. Another super dense line of code. Let's checkout how it works
1. You have a list of strings with random casing, and you want a list of strings that are all
lowercase.
[“Hi”, “mOm”, “aNd”, “DAD”] → [“hi”, “mom”, “and”, “dad”]
>>> random_case = [“Hi”, “mOm”, “aNd”, “DAD”] >>> # list comprehension here
3 Parts of a List Comprehension:
1) Expression
2) Item
3) Existing Collection
>>> random_case = [“Hi”, “mOm”, “aNd”, “DAD”] >>> all_lower = [s.lower() for s in random_case]
Let's break this down:
Expression: s.lower()
Item: s
Existing Collection: random_case
2. You have a string with all letters of the alphabet and you want a list where each element is a character in the alphabet.
>>> s = ‘abcdefghijklmnopqrstuvwxyz’ >>> # list comprehension here
3 Parts of a List Comprehension:
1) Expression
2) Item
3) Existing collection
>>> s = ‘abcdefghijklmnopqrstuvwxyz’ >>> alpha_lst = [ch for ch in s]
Let's break this down:
Expression: ch
(we do not make any changes to the character)
Item: ch
Existing Collection: s
3. You want a list of all numbers from 0-99. What existing collection can you use?
>>> range(100) >>> range(0, 100) >>> # list comprehension here
3 Parts of a List Comprehension:
1) Expression
2) Item
3) Existing collection
>>> range(100) >>> range(0, 100) >>> num_lst = [i for i in range(100)]
Let's break this down:
Expression: i
(we do not make any changes to the number)
Item: i
Existing Collection: range(100)
This is a common use for list comprehensions when you need properties of a list that range cannot do (such as append)
4. You have a list of temperatures in degrees celsius. You want that same list in degrees fahrenheit. You can convert from c to f by doing: temp_c * (9/5) + 32
.
>>> temps_c = [13, 14, 15, 16, 8, 9, 12] >>> # list comprehension here
3 Parts of a List Comprehension:
1) Expression
2) Item
3) Existing collection
>>> temps_c = [13, 14, 15, 16, 8, 9, 12] >>> temps_f = [t * (9/5) + 32 for t in temps_c]
Let's break this down:
Expression: t * (9/5) + 32
Item: t
Existing Collection: temps_c
Expression: t * (9/5) + 32
. This is getting a bit hard to read. Pro tip: we can decompose the expression into its own function and call the function on each item. (Note this is different than passing in only the name of the def like we do with map. Here we need to actually call the function on each item.)
>>> def c_to_f(temp_c): return temp_c * (9/5) + 32 >>> temps_c = [13, 14, 15, 16, 8, 9, 12] >>> temps_f = [c_to_f(t) for t in temps_c]
1. You have a list of numbers and you want only the even numbers in that list.
>>> random_nums = [3, 5, 4, 2, 9, 8, 16] >>> # list comprehension here
4 Parts of a List Comprehension:
1) Expression
2) Item
3) Existing collection
4) Condition
>>> random_nums = [3, 5, 4, 2, 9, 8, 16] >>> even_nums = [n for n in random_nums if n % 2 == 0]
Let's break this down:
Expression: n
(we do not make any changes to the number)
Item: n
Existing Collection: random_nums
Condition: if n % 2 == 0
2 (part a). You have a list of strings representing names, and you want a list of only the names that end in 'y'.
>>> kids = [“jonathan”, “isabelle”, “henry”, “juliette”, “audrey”, “bailey”] >>> # list comprehension here
4 Parts of a List Comprehension:
1) Expression
2) Item
3) Existing collection
4) Condition
>>> kids = [“jonathan”, “isabelle”, “henry”, “juliette”, “audrey”, “bailey”] >>> y_at_end = [name for name in kids if name[-1] == 'y']
Let's break this down:
Expression: name
(we do not make any changes to the name)
Item: name
Existing Collection: kids
Condition: if name[-1] == 'y'
Note: a list comprehension makes a new list and does not modify the original one.
2 (part b). You have a list of strings representing names and you want a list of only the names that start with 'b'.
>>> kids [“jonathan”, “isabelle”, “henry”, “juliette”, “audrey”, “bailey”] >>> # note that kids has not been changed by the earlier comprehenions >>> # list comprehension here
>>> b_at_front = [name for name in kids if name[0] == 'b']
Let's break this down:
Expression: name
(we do not make any changes to the name)
Item: name
Existing Collection: kids
Condition: if name[0] == 'b'
1. Given a list of strings lst, generate a new list of strings that contains only the strings in lst that have an even length.
>>> lst = [“I”, “love”, “CS106A”, “and”, “python”] >>> # list comprehension here
2. Generate a list of all numbers from 0-999 that are divisible by 8.
>>> # some existing collection >>> # list comprehension here
3. (Challenge) Given a list of strings, generate a list of tuples where the first value is the original string and the second value is the length of that string.
>>> my_thoughts = [“I”, “love”, “CS106A”] >>> # list comprehension that generates: [(“I”, 1), (“love”, 4), (“CS106A”, 6)]
Guiding question: what tools do we have to develop and analyze data?
Follow along with the example. Country Analysis Files
Example of a JSON dict with list, strings, ints, and booleans
{"food": "apple", "nums": [1, 2, 3, 4], "is_raining": true}
Differences from Python data structures:
strings must use double quotes
Booleans are lower case (true/false)
# foo.json file
{"food": "apple", "nums": [1, 2, 3, 4], "is_raining": true}
# example.py file to read the json # pull in the json module import json # this reads the json text out of f # arranges it as a python dict/list/etc data structure and stores in d with open('foo.json') as f: d = json.load(f)
Now d is the data structure described in the JSON text now stored in Python. In this case, d is the dictionary:
d['word'] -> 'apple' d['nums'] -> [1, 2, 3, 4] d['is_raining'] -> True
import json data = {‘cs106a’: ‘awesome’, ‘cs106b’: ‘also awesome’} with open('output.json', ‘w’) as f: json.dump(data, f)
import matplotlib.pyplot as plt # x = list of x vals # y = list of y vals plt.plot(x, y) # creates a line plot plt.scatter(x, y) # scatter plot plt.bar(x, y) # bar plot plt.title(text) # adds a title to the plot plt.ylabel('Label for the Y axis') # adds a label to the y axis plt.show() # displays the plot