Keys and Values

keys()
The 
dictionary 
method 
keys() 
can 
be 
used 
to 
print 
all 
the 
keys 
of 
a 
dictionary.
 
values()
Similarly, 
the 
method 
values() 
can 
be 
used 
to 
print 
all 
the 
values 
of 
a 
dictionary.
dict_name.keys()
dict_name.values()
dict_1 = { 'a' : 1, 'b' : 2 }
print(dict_1.keys())
print(dict_1.values())

dict() Function

The 
dict()
 
method 
is 
used 
to 
create 
a 
dictionary. 
We 
pass 
a 
list 
of 
tuples 
as 
arguments 
inside 
the 
function, 
each 
tuple 
has 
two 
elements 
that 
are 
taken 
as 
key-value 
pairs. 
If 
no 
arguments
 
are 
given 
inside 
the 
bracket, 
an 
empty
 
dictionary
 
is 
created.
dict_name = dict()
#empty dictionary
dict_name = dict( [(key_1, value_1), (key_2,value_2)])
#dictionary with key-value pairs
dict_empty = dict()
dict_new = dict( [(1,'a'), (2,'b')] )
print(dict_empty)
print(dict_new)

update() Method

The 
update()
 
method 
is 
used 
to 
change 
the 
value 
of 
an 
existing 
key, 
or 
add 
a 
new 
key-value 
pair 
to 
the 
dictionary.
dict_name.update({ key : value })
dict_1 = { 'a' : 1, 'b' : 2 }
dict_1.update({'b' : 3, 'c' : 4})
print(dict_1)

pop() Method

The 
pop() 
method 
is 
used 
to 
remove
 
a 
key-value 
pair 
from 
a 
dictionary. 
We 
can 
store 
the 
value 
of 
this 
pair 
and 
use 
it 
in 
a 
variable 
as 
well.
A 
default
 
value 
can 
be 
given, 
in 
case 
the 
key 
you 
are 
trying 
to 
remove 
does
 
not
 
exist
 
in 
the 
dictionary.
dict_name.pop(key, default_value)
dict_1 = { 'a' : 1, 'b' : 2 }
deleted_value = dict_1.pop('a')
default_value = dict_1.pop('z', -5)
print(deleted_value)
print(default_value)
print(dict_1)

get() Function

The 
get()
 
method 
is 
used 
to 
access
 
the 
value 
using 
the 
key. 
It 
is 
helpful 
because 
in 
case 
the 
key 
doesn't 
exist, 
a 
default 
value 
can 
be 
used.
dict_name.get(key, default_value)
dict_1 = { 'a' : 4, 'b' : 8 }
value = dict_1.get('a')
default_value = dict_1.get('z', -5)
print(value)
print(default_value)
print(dict_1)