You are on page 1of 3

ITPC FINALS REVIEWER Deleting Elements from an get method: gets a value

Existing Dictionary associated with specified key


DICTIONARIES AND SETS from the dictionary
To delete a key-value pair:
Dictionary: object that stores Format: dictionary.get(key,
a collection of data del dictionary[key]
default)
Each element consists of a key If key is not in the dictionary,
default is returned if key is not
and a value Often referred to as KeyError exception is raised
found
mapping of key to value Key
Getting the Number of Alternative to [] operator
must be an immutable object
Elements and Mixing Data
To retrieve a specific value, use Types Cannot raise KeyError
the key associated with it exception
len function: used to obtain
Format for creating a dictionary number of elements in a items method: returns all the
dictionary dictionaries keys and
dictionary = associated values
Keys must be immutable
{key1:val1, key2:val2} objects, but associated Format: dictionary.items()
Retrieving a Value from a values can be any type of
Returned as a dictionary view
Dictionary object
Each element in dictionary view
Elements in dictionary are One dictionary can include keys
is a tuple which contains a key
unsorted of several different immutable
and its associated value
types
General format for retrieving Use a for loop to iterate over
value from dictionary: Values stored in a single
the tuples in the sequence
dictionary[key] dictionary can be of different
types Can use a variable which
If key in the dictionary, receives a tuple, or can use two
associated value is returned, Creating an Empty Dictionary
variables which receive key and
otherwise, KeyError exception and Using for Loop to Iterate
value
is raised Over a Dictionary
keys method: returns all the
Test whether a key is in a To create an empty
dictionaries keys as a
dictionary using the in and dictionary:
sequence
not in operators Use {}
Format: dictionary.keys()
Helps prevent KeyError Use built-in function dict()
exceptions pop method: returns value
Elements can be added to the associated with specified key
Adding Elements to an dictionary as program executes and removes that key-value
Existing Dictionary pair from the dictionary
Use a for loop to iterate over
Dictionaries are mutable a dictionary Format: dictionary.pop(key,
objects default)
General format: for key in
To add a new key-value pair: dictionary: default is returned if key is not
dictionary[key] = value If key found
Some Dictionary Methods
exists in the dictionary, the popitem method: returns a
value associated with it will be clear method: deletes all the
randomly selected key-value
changed elements in a dictionary,
pair and removes that key-
leaving it empty
value pair from the dictionary
Format: dictionary.clear()
Format: dictionary.popitem()
Key-value pair returned as a If argument contains duplicates, Similarly, the not in operator
tuple only one of the duplicates will can be used to test whether a
appear in the set value does not exist in a set
values method: returns all
the dictionaries values as a Getting the Number of and Finding the Union of Sets
sequence Adding Elements
Union of two sets: a set that
Format: dictionary.values() len function: returns the contains all the elements of
number of elements in the set both sets
Use a for loop to iterate over
the values Sets are mutable objects To find the union of two sets:
add method: adds an element Use the union method
to a set
Format: set1.union(set2)
update method: adds a group
Use the | operator
of elements to a set
Format: set1 | set2
Argument must be a sequence
containing iterable elements, Both techniques return a new
and each of the elements is set which contains the union of
added to the set both sets
Deleting Elements from a Set Finding the Intersection of
Sets Sets
remove and discard
Set: object that stores a methods: remove the Intersection of two sets: a set
collection of data in same specified item from the set that contains only the
way as mathematical set elements found in both sets
The item that should be
All items must be unique Set is removed is passed to both To find the intersection of
unordered methods as an argument two sets:

Elements can be of different Behave differently when the Use the intersection method
data types specified item is not found in
the set Format: set1.intersection(set2)
Creating a Set
remove method raises a Use the & operator
set function: used to create a KeyError exception Format: set1 & set2
set
discard method does not raise Both techniques return a new
For empty set, call set() an exception set which contains the
For non-empty set, call clear method: clears all the intersection of both sets
set(argument) where argument elements of the set Finding the Difference of Sets
is an object that contains
iterable elements Using the for Loop, in, and Difference of two sets: a set
not in Operators with a Set that contains the elements
e.g., argument can be a list,
A for loop can be used to that appear in the first set but
string, or tuple
iterate over elements in a set do not appear in the second
If argument is a string, each set
character becomes a set General format: for item in set:
To find the difference of two
element The loop iterates once for each sets:
For set of strings, pass them to element in the set
Use the difference method
the function as a list The in operator can be used
to test whether a value exists Format: set1.difference(set2)
in a set
Use the - operator Set A is superset of set B if it
contains all the elements of
Format: set1 - set2
set B
Finding the Symmetric
To determine whether set A
Difference of Sets
is superset of set B
Symmetric difference of two Use the issuperset method
sets: a set that contains the
elements that are not shared Format: setA.issuperset(setB)
by the two sets
Use the >= operator
To find the symmetric
Format: setA >= setB
difference of two sets:
Serializing Objects
Use the symmetric_difference
method Serialize an object: convert
the object to a stream of
Format:
bytes that can easily be
set1.symmetric_difference(set2
stored in a file
)
Pickling: serializing an object
Use the ^ operator
Format: set1 ^ set2 To pickle an object: Import
the pickle module
Finding Subsets and
Open a file for binary writing
Supersets
Call the pickle.dump function
Set A is subset of set B if all
the elements in set A are Format: pickle.dump(object,
included in set B file)
To determine whether set A Close the file
is subset of set B
You can pickle multiple
Use the issubset method objects to one file prior to
closing the file
Format: setA.issubset(setB)
Use the <= operator Unpickling: retrieving pickled
object
Format: setA <= setB
To unpickle an object:
Import the pickle module
Open a file for binary writing
Call the pickle.load function
Format: pickle.load(file)
Close the file
You can unpickle multiple
objects from the file

You might also like