- Dictionaries
- Sets
- Tuples
- Dictionaries contain key/value pairs in a mutable, unordered collection.
- Keys must be immutable and unique.
- Dictionaries are declared:
- Using curly braces
{}
dict()
constructor
- Using curly braces
- Syntax:
{key1 : value1, key2: value2, key3 : value3}
- Access values by using the key as an index:
d[key]
- This statement will throw an error if the key is not in the dictionary.
- Keys can be added to dictionaries by:
d[new_key] = new_value
- Note that if the key already exists in the dictionary, this syntax will reassign the value to this new_value
- The keys method will return a list of the keys in a dictionary
dct.keys()
- The values method will return a list of the values in a dictionary
dct.values()
- The items method will return a list of tuples that contain the key/ value pairs
dct.items()
- Membership for dictionaries
k in dct
k in dct.keys()
v in dct.values()
- Creating a for loop allows us to iterate through a dictionary’s keys
for key in dictionary:
print(key)
- Combining a for loop with the items method allows us to iterate through both the keys and the values of a dictionary:
for key, value in dictionary.items():
print(key, value)
- In order to remove key/value pairs, you can use the pop method or the del keyword
d.pop('some_key')
del d['some_key']
- Create a copy of the original dictionary before changing it by adding and deleting key/value pairs
dictionary.copy()
- To check and see if a specific key is in a dictionary, you can use the get method
- Syntax:
dct.get(key, default=None)
- Syntax:
states_caps_dict = {'Georgia': 'Atlanta', 'Colorado': 'Denver', 'Indiana': 'Indianapolis'}
d.get(‘Washington’, ‘Capital not found’)
To make a copy of a dictionary you need to use the .copy()
method
- Syntax:
dct.copy()
- Err on the side of making copies of dictionaries
- Make a dictionary called restaurant_types that has the following associated key-value pairs:
keys (restaurant name) | values (restaurant type) |
---|---|
Red Lobster | Seafood |
Burger King | Fast Food |
Safeway | Grocery Store |
- How do you find the restaurant type for
'Burger King'
? - What if you don't know whether or not
'Outback Steakhouse'
is in the restaurant_types dictionary - how would you go about trying to get it's restaurant type and make sure that you won't get an error? - Add to it the key-value pair, (
'Outback Steakhouse', 'Delicious!')
. - What if we want to change the restaurant type of
'Safeway'
to just'Grocery'
- how would you make that change?
Write a function that creates a dictionary where the keys are all the positive integers that are less than 20 and the values are the cube of the integer.
- Sets are unordered, mutable collections
- Sets will only contain unique elements
- Sets can be declared:
- Using the set constructor
set()
- Using the set constructor
- Be careful when declaring empty sets.
{}
defaults to dictionaries.
- Sets only hold unique elements.
- This property is useful for removing duplicates from lists and tuples
- Do this by casting the
list
ortuple
to aset
- The union and intersection methods in sets are similar to their mathematical analogues.
- The union is a set of all elements in two sets
- Syntax:
set1.union(set2)
- Syntax:
- The intersection is a set of all the elements that two sets have in common
Syntax:
set1.intersection(set2)
- The difference of two sets A - B contains all the elements of set A that are not contained in set B
- Syntax:
set1.difference(set2)
set1.difference(set2)
is different fromset2.difference(set1)
l1 = [1, 4, 7, 0, 2, 5, 8]
l2 = [1, 2, 3, 4, 9]
- What does the intersection of these two lists return?
- What does the the union of these two lists return?
- What does the difference of
l1 - l2
return? - What about
l2 - l1
?
- Tuples are ordered collections
- Tuples are very similar to list with two key differences:
- Tuples are immutable.
- Tuples are declared using parenthesis.
- We can index and slice tuples because they are ordered
- Tuples have two methods associated with them: count and index
- Tuples can be declared in three ways:
tup = (1, 2, 3)
tup = tuple([1, 2, 3])
tup = 1, 2, 3
- For single elements tuples:
tup = (1,)
tup = tuple([1])
tup = 1,
- Once a tuple is declared, it generally can’t be changed in anyway
- However, if an element of a tuple is mutable, the element can be changed
- Tuples hold references to all the objects they contain, rather than the objects themselves.
Write a function that has two arguments that are both tuples. Return a single tuple that is the combination of the two original tuples that skips every other element in reverse.
- Say
tuple1 = (12, 14, 16, 18)
andtuple2 = (3, 5, 7, 9)
: - The result would be
(9, 5, 18, 14)
def function_name(tuple1, tuple2):
pass