Chapter 8: Dictionaries And Sets Flashcards
In a dictionary, you specify a unique ____ to associate with each _____
Key,value
How to create a empty dictionary
Empty_dic = {}
How to create a dictionary (not empty)
Acme_customer = {‘first’:’Wile’, ‘middle’: ‘E’, ‘last’: ‘Coyote’}
You can also use the ____ function to convert two-value sequences into a dictionary
Dict()
- a list of two items lists
A list of two-item tuple
- a list of two character strings
A tuple of two-character strings tos = (‘ab,’cd’,ef’)
To add an item, refer to the item by its key and assign a ______. If the key is already present, the existing value is______ by the new one, If it is new, it is ____.
Value,replaced,added
Pythons[‘Gilliam’] = ‘Gerry’
How to get a value given a key
Get()
Ex:
Some_pythons.get(‘John’)
This function also you get get all keys in dictionary
Keys()
Allows you yo get all values in dictionary
Values()
When you want to get all key-value pairs from dictionary use ____
Items()
When using items(), each key and value is returned as a ___
Tuple
Get the length of dictionary
Len()
Combine dictionaries using _____
{a,b}
A = first dictionary
B = second dictionary
You can use the ____ function to copy the keys and values of one dictionary into another
Update()
Delete an item by key with ___
Del
Del pythons[‘Marx’]
If you give ___ a key and it exists in dictionary, it returns the matching value and delete the key value pair. If it does not exist, it raises an exception
Pop()
How to delete all items in dictionary
Clear()
The ____ function copy’s key and values from one dictionary to another
Copy()
Iterate over keys in dictionary
For card in accusation \or for card in accusation.keys()
Iterate over values rather than keys
For value in accusation.values()
To return both key and tuple using for loop
For item in accusation.items()
A set is like a dictionary with its _____ thrown away leaving only ____
Values, keys
The ___/____ set is a set with zero items
Null,empty
To create a set, you use the ____ function to enclose one or more comma separated value in curly brackets
Even_numbers = {0,2,4,6}
You can create a set from a list, string, tuple, or dictionary using _____
Set()
Throw another item into a set with the set ____ method
Add()
You can delete an item from a set using the function _____
Remove()
You can iterate over the items in a set using ____ and _____
For,in
You can test for a value with ____
In
Ex:
If ‘vodka’ in contents:
Set intersection operator
&
Exams of combinations and operators
Ex:
For name,contents in drinks.item()
If ‘vodka’ in content and not content & {‘vermouth’,’cream’}
Print(name)
Intersection function
Intersenction()
In this example, get the union by using __ or the set ____ function
|, union()
The exclusive or uses ___ or ______ function
^, symmetric_difference()
You can check whether on set is a subset of another by using ___ or ____
<=, issubset()
A superset is opposite of subset and uses ____ or _____
> =, issuperset()
If you want to create a set that can’t be changed, call the ____ function with any iterable argument
Frozenset()
Frozenset([3,2,1])