Professional Documents
Culture Documents
Learning Outcomes:
What is a dictionary
How it is created
How to add, remove, and change elements of a dictionary
Properties of dictionaries:
curly brackets are used for dictionaries in Python
elements are key-value pairs
unordered container
elements are indexed using keys
mutable container
can contain elements of a different data type
1) Empty dictionary
In [ ]: '''The syntax for creating a dictionary is similar to that for creating a lis
t,
but we use curly brackets rather than square ones.'''
a = {} # empty dctionary
a
localhost:8888/nbconvert/html/Dictionary_Week4.ipynb?download=false 1/8
5/10/2020 Dictionary_Week4
# output that the nucleotides and their counts are stored together
# in the a variable:
gencode = {
'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M',
'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT':'T',
'AAC':'N', 'AAT':'N', 'AAA':'K', 'AAG':'K',
'AGC':'S', 'AGT':'S', 'AGA':'R', 'AGG':'R',
'CTA':'L', 'CTC':'L', 'CTG':'L', 'CTT':'L',
'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCT':'P',
'CAC':'H', 'CAT':'H', 'CAA':'Q', 'CAG':'Q',
'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGT':'R',
'GTA':'V', 'GTC':'V', 'GTG':'V', 'GTT':'V',
'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCT':'A',
'GAC':'D', 'GAT':'D', 'GAA':'E', 'GAG':'E',
'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGT':'G',
'TCA':'S', 'TCC':'S', 'TCG':'S', 'TCT':'S',
'TTC':'F', 'TTT':'F', 'TTA':'L', 'TTG':'L',
'TAC':'Y', 'TAT':'Y', 'TAA':'_', 'TAG':'_',
'TGC':'C', 'TGT':'C', 'TGA':'_', 'TGG':'W'}
gencode
localhost:8888/nbconvert/html/Dictionary_Week4.ipynb?download=false 2/8
5/10/2020 Dictionary_Week4
3) Creating a Dictionary from multiple keys and initializing all with the same
value
Out[42]: 3
localhost:8888/nbconvert/html/Dictionary_Week4.ipynb?download=false 3/8
5/10/2020 Dictionary_Week4
The value of A is = 3
The value of U is = None
The value of U is = 0
The value of U is = []
In [48]: # you can also use keys() method to get all keys in a dictionary
a = {'A':3, 'G':0, 'C':5, 'T':1}
a.keys()
In [49]: # you can also use values() method to get all values in a dictionary
a = {'A':3, 'G':0, 'C':5, 'T':1}
a.values()
In [50]: # you can also use items() method to get all keys-value pairs
a = {'A':3, 'G':0, 'C':5, 'T':1}
a.items()
localhost:8888/nbconvert/html/Dictionary_Week4.ipynb?download=false 4/8
5/10/2020 Dictionary_Week4
In [ ]: # we can print keys and values as well with slight modification in above loop
a = {'A':3, 'G':0, 'C':5, 'T':1}
for k in a:
print (k, a[k])
Adding elements
In [ ]: dna = {}
dna['A'] = 1 # this procedure adds a single element at a time
dna
To add one or more than one elements we can use update method
In [ ]: dna = {}
dna.update({'A':1})
print ("dna after adding a single element = ", dna)
dna.update({'G':3, 'T':5})
print ("dna after adding more than one elements = ", dna)
localhost:8888/nbconvert/html/Dictionary_Week4.ipynb?download=false 5/8
5/10/2020 Dictionary_Week4
In [ ]: a = {}
a['one'] = 1
a['two'] = 3.0
a['three'] = []
a['four'] = ()
a['five'] = "ATGC"
a['six'] = []
a
# Note we can store duplicate values in dictionary with different key names
# Here we have two keys six and three containing empty list
Each key can occur only once. Dictionary holds unique elements only
a['test'] = ''
print ('Adding string as a key = ', a)
a[[4,5]] = ''
print ('Adding list as a key = ', a)
# Note: if we execute this code, we can see tuples and strings are added succe
ssfully.
# However, list can't be used as a key
1) del method
The del statement removes a key-value pair from the dictionary. It’s similar to del in list except that it
accepts key not index.
#Note: we will get key error if that key is not present in the dictionary
# We can use membership testing to avoid error in case if key is not present
# The del keyword can also delete the dictionary completely if only dictionary
name is used
2) clear() method
removes all elements from the dictionary.
3) pop(key) method
removes the item with the specified key name
we will get the value of the deleted key
localhost:8888/nbconvert/html/Dictionary_Week4.ipynb?download=false 7/8
5/10/2020 Dictionary_Week4
In [ ]: # it will also raise an error if the key is not present in the dictionary
dna = {'A':5, 'T':3, 'C': 1, 'G':0}
dna.pop('a')
4) popitem() method
removes the last element that was inserted in the dictionary
In [ ]: a = {}
a['one'] = 1
a['two'] = 3.0
a['three'] = []
a['four'] = ()
a['five'] = "ATGC"
a['six'] = []
# Note: six was the last element that was inserted in the dictionary. It is re
moved.
del a['A']
localhost:8888/nbconvert/html/Dictionary_Week4.ipynb?download=false 8/8