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