+
+
+ More with lists!
+
+
+ Tips for navigating the slides:
+
+ - Press O or Escape for overview mode.
+ - Visit this link for a nice printable version
+ - Press the copy icon on the upper right of code blocks to copy the code
+
+
+
+
+
+ Class outline:
+
+
+ - Review
+
- More list functionality
+
- Nested lists
+
- Getting data from CSV files
+
+
+
+
+ Data structures
+
+ A data structure is a particular way of organizing data in a computer so that it can be used effectively.
+
+ Data structures in programming are a bit like lists and tables in text docs/presentations - we use them to organize information and make it more meaningful/useful.
+
+
+
+
+ Lists
+ A list is a container that holds a sequence of related pieces of information.
+ The shortest list is an empty list, just 2 square brackets:
+
+ members = []
+
+ Lists can hold any Python values, separated by commas:
+
+ # all strings
+ members = ["Liz", "Tinu", "Brenda", "Kaya"]
+ # all integers
+ ages_of_kids = [1, 2, 7]
+ # all floats
+ prices = [79.99, 49.99, 89.99]
+ # mixed types
+ mixed_types = ["Liz", 7, 79.99]
+
+
+
+ Accessing items (Bracket notation)
+
+ Lists are ordered. Each list item has an index, starting from 0.
+
+
+ temps = [48.0, 30.5, 20.2, 99.0, 52.0]
+ # Index: 0 1 2 3 4
+
+
+ Access each item by putting the index in brackets:
+
+ temps[0] # 48.0
+ temps[1] # 30.5
+ temps[2] # 20.2
+ temps[3] # 99.0
+ temps[4] # 52.0
+ temps[5] # π« Error!
+
+
+
+ List length
+
+ Use the global len()
function
+ to find the length of a list.
+
+
+ attendees = ["Tammy", "Shonda", "Tina"]
+
+ print(len(attendees)) # 3
+
+ num_of_attendees = len(attendees)
+ print(num_of_attendees)
+
+
+
+ Modifying list items
+
+ Python lists are mutable, which means
+ you can change the values stored in them.
+
+ Use bracket notation to modify single items in the list:
+
+ temps = [48.0, 30.5, 20.2, 99.0, 52.0]
+ temps[2] = 22.22
+ temps[-1] = 55.55
+
+
+
+
+ Modifying lists with methods
+
+
+
+
+ Method
+ | Description
+ |
+
+ append(item)
+ | Adds item to the end of the list. This increases list length by one.
+ |
+ insert(index, item)
+ | Inserts item at the specified index . This increases the list length by one and
+ shifts all items after the specified index forward by one index.
+ |
+ remove(item)
+ | Remove the first item from the list whose value is equal to item .
+ It raises a ValueError if there is no such item.
+ |
+ index(item)
+ | Returns the index of the first occurrence of an item whose value is equal to item .
+ It raises a ValueError if there is no such item.
+ |
+ pop([index])
+ | If no index is specified, it removes the last item from the list and returns it.
+ Otherwise, it removes the item at the specified index and returns it.
+ |
+
+ See more in Python.org documentation
+
+
+
+ List method return values
+
+ Most of the list mutation methods perform an action and don't return a value.
+ An exception is .pop()
, which removes a list item and returns its value, so you can assign it to a variable.
+
+ groceries = ["apples", "bananas"]
+
+ bought_food = groceries.pop()
+
+
+
+ For loop
+ The other type of loop supported in Python is a for
loop
+ The for loop syntax:
+
+
+ for <value> in <sequence>:
+ <statement>
+ <statement>
+
+
+ For loops provide a clean way of repeating code for each item in a data structure (like a list).
+
+
+ Looping through a list
+ π For loops love lists!
+
+ scores = [80, 95, 78, 92]
+ total = 0
+ for score in scores:
+ total += score
+
+
+
This does the same thing as a while
loop with a counter, but with less code.
+
+ scores = [80, 95, 78, 92]
+ total = 0
+ i = 0
+ while i < len(scores):
+ score = scores[i]
+ total += score
+ i += 1
+
+
+
+
+
+ More list functionality
+
+
+
+ Checking if value is in a list
+
+ The in
keyword checks if
+ a value is in a list (or any sequence).
+
+
+ if "bananas" in groceries:
+ print("Watch your step!")
+
+ if "bleach" in groceries:
+ print("Watch what you eat!")
+
+
+ When used inside if conditions, in
expressions return a Boolean (True
/False
).
+
+
+ Helpful list methods
+
+
+
+
+ Method
+ | Description
+ |
+
+ count(item)
+ | Returns the number of occurrences of item in the list. If item is not found, returns 0.
+ |
+ reverse()
+ | Reverses the elements of the list in place.
+ |
+ sort([key=function])
+ | Sorts the items of the list in place. The optional key argument specifies
+ a function to compute a comparison key for each item in the list. Otherwise, items are compared based on their values.
+ |
+
+ See more in Python.org documentation
+
+
+
+ Examples with reverse
+
+ If we start with...
+
+ nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+ spelling = ["B", "U", "M", "B", "L", "E"]
+
+
+ Then call the reverse
method...
+
+ nums.reverse() # [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
+ spelling.reverse() # ["E", "L", "B", "M", "U", "B"]
+
+
+
+
What do you think of this?
+
+
+ nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+ reversed_nums = nums.reverse()
+
+
+
It's a bit strange, since reverse()
+ does an in-place reversal, so now the program has the same list in two variables!
+
+
+
+
+
+ Examples with sort
+
+
+ foods = ["orange", "apple", "Pear", "banana"]
+
+
+ The simplest sort:
+
+ foods.sort()
+ # ["Pear", "apple", "banana", "orange"]
+
+
+ Specifying a key argument of an existing function:
+
+ foods.sort(key=str.lower)
+ # ["apple", "banana", "orange", "Pear"]
+
+
+ .sort()
modifies a list in place. To sort a list into a new list, use the global sorted()
function.
+
+
+
+ Useful functions for lists
+
+ These are functions, not methods.
+
+
+
+ Module
+ | Function
+ | Description
+ |
+
+ Global
+ | min(list, [key=function])
+ | Returns the minimum value in list . The optional key argument specifies a custom comparator function.
+ |
+ Global
+ | max(list, [key=function])
+ | Returns the maximum value in list . he optional key argument specifies a custom comparator function.
+ |
+ random
+ | choice(list)
+ | Returns a randomly selected item from list .
+ |
+ random
+ | shuffle(list)
+ | Shuffles the elements of the list in place.
+ |
+
+
+
+ Nested Lists
+
+ Since Python lists can contain any values, an item can itself be a list.
+
+
+
+ gymnasts = [
+ ["Brittany", 9.15, 9.4, 9.3, 9.2], # gymnasts[0]
+ ["Lea", 9, 8.8, 9.1, 9.5], # gymnasts[1]
+ ["Maya", 9.2, 8.7, 9.2, 8.8] # gymnasts[2]
+ ]
+
+ You can think of nested lists like rows in a table:
+
+
+
+ Nested Lists
+
+ gymnasts = [
+ ["Brittany", 9.15, 9.4, 9.3, 9.2], # gymnasts[0]
+ ["Lea", 9, 8.8, 9.1, 9.5], # gymnasts[1]
+ ["Maya", 9.2, 8.7, 9.2, 8.8] # gymnasts[2]
+ ]
+
+
+ - What's the length of
gymnasts
? 3
+ - What's the length of
gymnasts[0]
? 5
+
+
+
+ Accessing nested list items
+
+
+ gymnasts = [
+ ["Brittany", 9.15, 9.4, 9.3, 9.2],
+ ["Lea", 9, 8.8, 9.1, 9.5],
+ ["Maya", 9.2, 8.7, 9.2, 8.8]
+ ]
+
+
+ Access using bracket notation, with more brackets as needed:
+
+ gymnasts[0] # ["Brittany", 9.15, 9.4, 9.3, 9.2]
+ gymnasts[0][0] # "Brittany"
+ gymnasts[1][0] # "Lea"
+ gymnasts[1][4] # 9.5
+ gymnasts[1][5] # π« IndexError!
+ gymnasts[3][0] # π« IndexError!
+
+
+
+ Modifying nested lists: Bracket notation
+
+ gymnasts = [
+ ["Brittany", 9.15, 9.4, 9.3, 9.2],
+ ["Lea", 9, 8.8, 9.1, 9.5],
+ ["Maya", 9.2, 8.7, 9.2, 8.8]
+ ]
+
+ Modify using bracket notation as well, just like before:
+
+ gymnasts[0] = ["Olivia", 8.75, 9.1, 9.6, 9.8]
+ gymnasts[1][0] = "Leah"
+ gymnasts[2][4] = 9.8
+
+
+
+ Modifying nested lists: List methods
+ If we start with...
+
+ gymnasts = [
+ ["Brittany", 9.15, 9.4, 9.3, 9.2],
+ ["Lea", 9, 8.8, 9.1, 9.5]
+ ]
+
+ We can append another list a few ways:
+
+ gymnasts.append(["Maya", 9.2, 8.7, 9.2, 8.8])
+
+
+ gymnasts += ["Maya", 9.2, 8.7, 9.2, 8.8]
+
+
+ gymnast = []
+ gymnast.append("Maya")
+ gymnast.append(9.2)
+ gymnast.append(8.7)
+ gymnast.append(9.2)
+ gymnast.append(8.8)
+ gymnasts.append(gymnast)
+
+
+
+
+ Looping through nested lists
+
+ gymnasts = [
+ ["Brittany", 9.15, 9.4, 9.3, 9.2],
+ ["Lea", 9, 8.8, 9.1, 9.5],
+ ["Maya", 9.2, 8.7, 9.2, 8.8]
+ ]
+
+ Use a nested for-in
loop:
+
+ for gymnast in gymnasts:
+ print(gymnast)
+ for data in gymnast:
+ print(data)
+
+ Remember what type of data is being stored in the loop variable!
+
+
+
+ Reading data from CSV files
+
+
+ CSV files
+ Comma Separated Value (CSV) is a plain text format used to store "tabular" data (aka rows and columns), like from a spreadsheet or database table.
+ Because CSV is simple and non-proprietary, it's commonly used to exchange data between different applications.
+
+ Brittany, 9.15, 9.4, 9.3, 9.2
+ Lea, 9, 8.8, 9.1, 9.5
+ Maya, 9.2, 8.7, 9.2, 8.8
+
+
+
+
+ Opening files
+ We use the Python built in open()
method to open files for reading or writing.
+ The keyword with
tells Python to automatically close the file when we're finished.
+
+ with open('filename') as my_file:
+ # do something
+
+
+
+ Reading files with CSV module
+ Python has a built-in CSV module that includes many features used for reading/writing CSV files.
+ Open a CSV and read its contents
+
+ # import the csv module
+ import csv
+ # open the file
+ with open('fun_spreadsheet.csv') as my_file:
+ # load the contents of the CSV file into a variable
+ csv_reader = csv.reader(my_file, delimiter=',')
+ # CSV is a nested list!
+ # now we can loop through the rows in the file
+ for row in csv_reader:
+ print(row)
+
+
+
+ Reading files with CSV module
+ A CSV is a list of rows, which are lists of values.
+ We can use nested for loops to get values from rows and cells.
+
+ import csv
+ with open('fun_spreadsheet.csv') as my_file:
+ csv_reader = csv.reader(my_file, delimiter=',')
+ for row in csv_reader:
+ print(row)
+ for cell in row:
+ print(cell)
+
+
+
+
+