Chapter 10: Tuples Flashcards
A type where one value can be checked to see if it is greater than, less than, or equal to another value of the same type. Can be put in a list and sorted.
comparable
A collection of related values, often organized in lists, dictionaries, tuples, etc.
data structure
A pattern that involves building a list of tuples, sorting, and extracting part of the result.
DSU
The operation of assembling a variable-length argument tuple.
gather/
tuple packing
A type that has a hash function. Immutable types like integers, floats, and strings are this; mutable types like lists and dictionaries are not.
hashable
The operation of treating a sequence as a list of arguments.
scatter
A summary of the type, size, and composition of a data structure.
shape
(of a data structure)
A list (or other sequence) with a single element.
singleton
An immutable sequence of elements.
Can be any type
Indexed by integers and can be sliced
Comparable and hashable (can sort lists of them and use as key values in dictionaries)
tuple
A technique where a sequence on the right is assigned to multiple variables on the left at the same time. The right side is evaluated and then its elements are assigned to the variables on the left.
Also works with lists that have same number of items
tuple unpacking/ assignment
x, y = (1, 2)
x=1
y=2
tuple notation
t = ‘a’, ‘b’, ‘c’
OR
t = (‘a’, ‘b’, ‘c’)
t1 = (‘a’,)
function to create tuple
tuple()
tuple(‘word’) = (‘w’, ‘o’, ‘r’, ‘d’)
tuple([list]) = (item1, item2, item3)
method to create modified tuple with changes by adding new tuple with slice of original tuple
tuple replacement
t = (‘a’, ‘b’, ‘c’)
t = (‘A’,) + t[1:] = (‘A’, b’, ‘c’)
using operators to compare sequential tuple elements. Returns true or false depending on first mismatching pair of values
tuple comparison
building a LIST of tuples with one or more sort keys (eg. length of word) preceding the elements (eg. word) from the sequence
Decorate
(DSU)
use list method to put list of tuples in order. Compares 1st element in tuple pair, only looks at 2nd element to break ties
Sort
list.sort()
(DSU)
extract the sorted sequence elements
Undecorate
(DSU)
method format to sort list in decreasing order
list.sort(reverse=True)
swap values of two variables in a single statement using tuples
can use with any kind of sequence
form of tuple unpacking
a, b = b, a
left side= variables. right side= expressions
ride side evaluated before assigned to left side variables
dictionary method that returns a list of tuples (key-value pairs)
way to output contents of dictionary sorted by key
.items()
t = dict_name.items()
term meaning no particular order
hash key order
built-in function that takes sequence as parameter and returns same elements in opposite order
needs list() function before it
list(reversed())
built-in function that takes sequence as parameter and returns same elements in order
does NOT need list() function before it
sorted()
way to create a list from another list
list comprehension
int_strings = [‘42’, ‘65’, ‘12’]
list_of_ints = [int(x) for x in int_strings ]
print(sum(list_of_ints))
errors caused when a data structure has the wrong type, size, or composition or the wrong type introduced in the code
shape error
function that constructs or creates object instances
eg. tuple function constructs tuples
constructor function