Dictionary Methods in Python

Dictionary Methods in Python 

 
Dictionary Methods in Python

Python has build in methods to work with dictionaries.

clear()
copy()
fromkeys()
get()
items()
keys()
pop()
popitem()
setdefault()
update()
values()


clear() dictionary.clear()
Removes all the elements from the dictionary 
phonebook = {'John': '000001', 'Peter': '000002', 'Rachel': '000003'}

phonebook.clear()

print(phonebook)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{}
 

copy() dictionary.copy()
Returns a copy of the dictionary 
mydict= {'key1': 0, 'key2': 0, 'key3': 0}

newdict= mydict.copy()

print(newdict)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'key1': 0, 'key2': 0, 'key3': 0}
 

fromkeys() dict.fromkeys(keys, value)
Returns a dictionary with the specified keys and value 
key = ('key1', 'key2', 'key3')
value = 0

mydict= dict.fromkeys(key, value)

print(mydict)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'key1': 0, 'key2': 0, 'key3': 0}
 

get() dictionary.get(keyname, value)
Returns the value of the specified key 
phonebook = {'John': '000001', 'Peter': '000002', 'Rachel': '000003'}

number = phonebook.get('Peter')

print(number)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

000002
 

items() dictionary.items()
Returns a list containing a tuple for each key value pair 
phonebook = {'John': '000001', 'Peter': '000002', 'Rachel': '000003'}

x = phonebook.items()

print(x)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

dict_items([('John', '000001'), ('Peter', '000002'), ('Rachel', '000003')])
 
keys() dictionary.keys()
Returns a list containing the dictionary's keys 
phonebook = {'John': '000001', 'Peter': '000002', 'Rachel': '000003'}

x = phonebook.keys()

print(x)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

dict_keys(['John', 'Peter', 'Rachel'])
 

pop() dictionary.pop(keyname, defaultvalue)
Removes the element with the specified key

phonebook = {'John': '000001', 'Peter': '000002', 'Rachel': '000003'}

phonebook.pop("John")

print(phonebook)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'Peter': '000002', 'Rachel': '000003'}

popitem() dictionary.popitem()
Removes the last inserted key-value pair 
phonebook = {'John': '000001', 'Peter': '000002', 'Rachel': '000003'}

phonebook.popitem()

print(phonebook)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'John': '000001', 'Peter': '000002'}
 

setdefault() dictionary.setdefault(keyname, value)
Returns the value of the specified key. If the key does not exist: insert the key, with the specified value 
phonebook = {'John': '000001', 'Peter': '000002', 'Rachel': '000003'}

s= phonebook.setdefault('Rachel')

print(s)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

000003
 
If the key doesn't exist setdefault() will insert the key with value NONE
phonebook = {'John': '000001', 'Peter': '000002', 'Rachel': '000003'}

s= phonebook.setdefault('Marcin')

print(phonebook)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'John': '000001', 'Peter': '000002', 'Rachel': '000003', 'Marcin': None}
You can also add the key with value if doesn't exist.
phonebook = {'John': '000001', 'Peter': '000002', 'Rachel': '000003'}

s= phonebook.setdefault('Marcin', 1234567)

print(phonebook)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'John': '000001', 'Peter': '000002', 'Rachel': '000003', 'Marcin': 1234567}

update() dictionary.update(iterable)
Updates the dictionary with the specified key-value pairs 
phonebook = {'Name': 'John', 'PhoneN0': '000002'}

phonebook.update({'Email': 'johnseamail@longjohn.com'})

print(phonebook)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'Name': 'John', 'PhoneN0': '000002', 'Email': 'johnseamail@longjohn.com'}
 

values() dictionary.values()
Returns a list of all the values in the dictionary 
phonebook = {'Name': 'John', 'PhoneN0': '000002', 'Email': 'johnseamail@longjohn.com'}

x = phonebook.values()

print(x)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

dict_values(['John', '000002', 'johnseamail@longjohn.com'])
 

Comments