Dictionaires and Sets Flashcards
(34 cards)
In dictionaries _____ does not matter
order
To create a dictionary, you place curly brackets ({}) around comma-separated key : value pairs. The simplest dictionary is an empty one, containing no keys or values at all:
> > > empty_dict = {}
empty_dict
{}
You can create a dictonary using the function _____
dict()
»> empty_dict = {}
»> empty_dict
{}
> > > pythons = {
… ‘Chapman’: ‘Graham’,
… ‘Cleese’: ‘John’,
… ‘Idle’: ‘Eric’,
… ‘Jones’: ‘Terry’,
… ‘Palin’: ‘Michael’,
… }
How to add (Gilliam, Gerry) to this list
> > > pythons[‘Gilliam’] = ‘Gerry’
pythons
{‘Chapman’: ‘Graham’, ‘Cleese’: ‘John’, ‘Idle’: ‘Eric’,
‘Jones’: ‘Terry’, ‘Palin’: ‘Michael’, ‘Gilliam’: ‘Gerry’}
Dictinoary Keys must be _____
unique
One way to key an item
[key]
»> some_pythons[‘John’]
‘Cleese’
Another way to get an item
get()
»> some_pythons.get(‘John’)
‘Cleese’
How to get all the key in a dictionary
keys()
How to get all the values in a dictionary
values()
How to get all the key-value pairs from dictonary
items()
»> list( signals.items() )
[(‘green’, ‘go’), (‘yellow’, ‘go faster’), (‘red’, ‘smile for the camera’)]
You can use the ____ funcrion to copy the keys and values from one dictonary to another
update()
Delte an item by key with ___
del
»> del pythons[‘Marx’]
If you give ____ a key and it exists in the dictionary, it returns the matching value and deletes the key-value pair. If it doesn’t exist, it raises an exception:
pop()
How to delete all item
clear()
Test for a key with ___
in
Dictionaries can be compared with the simple comparison operators ___, ____
==, !=
A set is like a dictionary wth values thrown away, leaving only ___
keys
Create an empty set
> > > empty_set = set()
Function that you can use to create a set from a list, strnig, tuple, or dictionary (discarding any duplciate values)
set()
»> set( ‘letters’ )
{‘l’, ‘r’, ‘s’, ‘t’, ‘e’}
Getting the length of a set
len()
How to add item to set
add()
How to delete an item in set
remove()
Intersection operator
&
The result of the & operator is a set that contains ____ of the items that appear in both lists that you compare. If neither of those ingredients were in contents, the & returns an empty set, which is considered ___.
all, False