Chapter 6 Collections Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Overview Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. List Literal Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Lists Due to Python’s dynamic nature, lists may contain values of unrelated types or have unrelated purpose. Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Lists If you can’t describe a list as a collection of one kind of thing your program design likely has a problem. Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Common List Operations Table 0.0.1: Common List Operations Operation Description x = [y_1, ..., y_n] Create a new list with y_1 ... y_n. x[i] x[i] = y Retrieve the value in x associated with index i. Set the value in x associated with index i to y. x[i:j] Create a new list with values associated with indexes i to j (exclusive) x.append(y) Append y to the list x. x.insert(i, y) Insert y into the list at index i. Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Common List Operations Table 0.0.1: Common List Operations Operation del x[i] Description Remove the entry in x associated with index i. len(x) Return the number of elements in x. list(x) Produces a list from sequence x. y in x Returns True if y is in the list x. Returns False otherwise. x.sort() Sort the items in x in place. x.reverse() Reverse the items in x in place. x+ y Concatenate the lists x and y to form a new list. Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. List Element Access Like strings, list elements are numbered sequentially starting at zero or using negative indexes from the end of the list. 0 1 2 3 4 ’ Fr ed’ ’ Daphne’ ’ Vel ma’ ’ Shaggy’ ’ Scooby’ -5 -4 -3 -2 -1 Using this convention we can access elements of a list much like we do for individual characters in a string. Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. List Element Access Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. List Slicing Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Changing Contents of a List Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Lists are Mutable Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Splitting Strings One really useful way of generating a list is by splitting a string into substrings. This is accomplished using the string split method. By default split uses whitespace to divide the substrings but different separators can be passed as an argument. Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Lists of Lists Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Lists of Lists Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Multi-Dimensional Lists Accessing individual elements of the matrix uses standard list notation but beware that the outer list represents the row and the inner list represents the column. Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Multi-Dimensional Lists If we changed the numbers in the matrix to X's and O's we could represent a Tic-TacToe board. Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Dictionaries Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Dictionaries Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Dictionaries Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Dictionaries Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Dictionaries Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Dictionary Operations Table 0.0.1: Common Dictionary Operations Operation Description x = {k_1: y_1, ..., Create a new dictionary with key value k_2: y_n] pairs (k_1, y_1) ... (k_n, y_n). x[k] x[k] = y del x[k] Retrieve the value in x associated with key k. Set the value in x associated with key k to y. Remove the entry in x associated with key k. Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Dictionary Operations Table 0.0.1: Common Dictionary Operations Operation Description len(x) Return the number of elements in x. y in x x.items() Returns True if the key y in the dictionary x. Returns False otherwise. Return an iterable sequence of key value pairs. x.keys( ) Return an iterable sequence of keys. x.values() Return an iterable sequence of values. Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Tuple Literal Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Tuples Tuples are formed like lists but using parenthesis instead of brackets: A single element tuple must have a trailing comma: Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Tuple Operations Table 0.0.1: Common Tuple Operations Operation Description x = (y_1, ..., y_n) Create a new tuple with y_1 ... y_n. x[i] x[i:j] Retrieve the value in x associated with index i. Create a new tuple with values associated with indexes i to j (exclusive) len(x) Return the number of elements in x. tuple(x) Produces a tuple from a sequence x. y in x x+ y Returns True if y is in the tuple x. Returns False otherwise. Concatenate the lists x and y to form a new list. Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Tuples Python also has some syntactic shorthand for tuples such that commas outside of a list or dictionary also define a tuple. Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Tuple Unpacking Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.