Tuples & Dictionaries (UCSD - Wk 2) Flashcards
how do you pronounce tuple
too pel
What is the difference between a tuple and a list?
A tuple is immutable (you can’t change any of the values)
What’s the difference in how you instantiate a tuple and a list?
list = [x, y]; tuple = (x, y); can also instantiate a tuple with a list with func: tupleVar = tuple(list)
Why is it important that it’s immutable for parallel computing?
Don’t need to account for multiple threads / people using diff data
Why is the immumtability important for using tuples with dictionaries?
Tuples can be used as keys in dict. since values don’t change, can org on initial value
What is the python equivalent of a map?
Dictionary
How can we think about the structure of a dictionary?
It’s like an index: Key Value Pairs, where Key is immutable (value or tuple)
What kinds of objects can you store in the value of a dictionary?
Any. ints, lists, even other dictionaries
how do you initialize a dictionary?
dict = {key1:value1, key2, value2}
how do you initialize a dict with a tuple as a key
dict = {(key1A, key1B): value1, … }
lookup value for key1 in dict
dict[ key1 ]
How do you add to a dictionary
create a new key / value pair. If key already in use, over-writes value
is there a fixed order for dictionaries?
no. (one of the reasons they’re fast to lookup)
What happens if you try to retrieve a value for a dict where the key does not exist?
get a run-time error
What’s a safer way to retrieve an element from a dictionary if you’re not sure if the key exists
x = dict.get(key); Returns None if key non-existent
Test to see if a key is in a dictionary
key1 in dict; returns bool
how to retrieve and delete value with key1 in dict
dict.pop(key1)
how to just delete value with key from dict
del dict[key1]
iterate over keys in a dictionary
for i in dict: print(i)
iterate over key values in a dictionary
for key, value in dict.items(): print(key, “:”, value)
can you change a dict while iterating through it?
no.
how do you remove a set of items from a dict with iteration?
iterate through appending keys to remove to a new list, then iterate through the list, popping items from orig dict
how to build a list with squares from 1 to 10 (eg of “comprehension”)
list = [i**2 for i in range (1,11)] > can use an iteration and values here
How to create a list with random numbers (after import random package)
list = [random.randint(0,5) for i in range (x,y)]
how do you build a dictionary using comprehension
dict = {key : value for i in range (0,10)}
What are some distinguishing features of sets
unique elements, un-ordered (allowing to be very fast), and useful math operations
how do you initialize a set
setVar = set([‘blue’, ‘green’, ‘red’]) or set(list)
how do you add an element to a set
setVar.add(value)»_space; if you add an element that already exists, no change (no error)
how do you discard an element from a set
setVar.discard(value)»_space; if not there, does nothing and no error
return all elements that in two sets
eitherSetVar = set1.union(set2)»_space; just returns all unique values
return elements that are common to both sets
bothSetVar = set1.intersect(set2)
Alternatives to union and intersect methods
OR ( | ) and AND ( & ) operators»_space; set1 | set2; set1 & set2