Python dictionary is an unordered
collection of items.
While other compound data types have
only value as an element, a dictionary has
a key: value pair.
Dictionaries are optimized to retrieve
values when the key is known.
Creating a dictionary is as simple as placing items inside
curly braces {} separated by comma.
Each element in a dictionary is represented by
a key:value pair.
While values can be of any data type and can repeat,
keys must be of immutable type and must be unique.
3
While indexing is used with other container types to access values,
dictionary uses keys. Key can be used either inside square brackets
or with the get() method.
The difference while using get() is that it returns None instead
of KeyError, if the key is not found.
Dictionary are mutable. We can add new items or
change the value of existing items using assignment
operator.
If the key is already present, value gets updated, else a
new key: value pair is added to the dictionary.
We can remove a particular item in a dictionary by using the method pop().
This method removes as item with the provided key and returns the value.
All the items can be removed at once using the clear() method.
We can also use the del keyword to remove individual items or the entire
dictionary itself.
The method, popitem() can be used to remove and return an
arbitrary item (key, value) form the dictionary.
Raises KeyError if the dictionary is empty.
We can test if a key is in a dictionary or not using the keyword in. Notice
that membership test is for keys only, not for values.
Using a for loop we can iterate through each
key in adictionary.
Method Description
clear() Remove all items form the dictionary.
copy() Return a shallow copy of the dictionary.
fromkeys(seq[, v]) Return a new dictionary with keys from seq and value equal to v(defaults to None).
get(key[,d]) Return the value of key. If key doesnot exit, return d (defaults to None).
items() Return a new view of the dictionary's items (key, value).
keys() Return a new view of the dictionary's keys.
Remove the item with key and return its value or d if key is not found. If d is not provided
pop(key[,d])
and key is not found, raises KeyError.
popitem() Remove and return an arbitary item (key, value). Raises KeyError if the dictionary is empty.
If key is in the dictionary, return its value. If not, insert key with a value of d and
setdefault(key[,d])
return d (defaults to None).
update([other]) Update the dictionary with the key/value pairs from other, overwriting existing keys.
values() Return a new view of the dictionary's values
Function Description
len() Return the length (the number of items) in the dictionary.
sorted() Return a new sorted list of keys in the dictionary.
# Definition of country and capital
countries = ['spain', 'france', 'germany', 'norway','india']
capitals = ['madrid', 'paris', 'berlin', 'oslo','delhi']
I would like to print the capital of germany.
# Get index of 'germany': ind_ger
# Use ind_ger to print out capital of Germany
If you know how to access a dictionary, you can also
assign a new value to it. To add a new key-value pair to
countryCapitals, you can use something like this:
countryCapitals[‘india’] = ‘delhi’
To remove a entry from dictionary, we can use del
method.
Remove norway
del (countryCapitals[‘norway’])
Create a Dictionary which records the major
cities of each state.
{state : [major cities] }
Dictionaries can contain key:value pairs
where the values are again dictionaries.