04 Python Dictionary
04 Python Dictionary
• The dictionary is a collection that contains key: value pairs separated by commas inside curly
brackets.
• Dictionary items are ordered (from version 3.7 onwards), changeable, and does not allow duplicates.
• Keys are immutable but values are mutable.
Note- While adding a value, if the key-value already exists, the value gets updated otherwise a new Key with
the value is added to the Dictionary.
Note: The del Dict will delete the entire dictionary and hence printing it after deletion will raise an Error.
del capitals[‘France’]
Popped_element = capitals.popitem()
Len Method
Python dictionary method len() gives the total length of the dictionary. This would be equal to the number of
items in the dictionary.
dict = {'Name': 'Zara', 'Age': 7};
print "Length : %d" % len (dict)
d.get(<key>[, <default>])
Returns the value for a key if it exists in the dictionary.
The Python dictionary .get() method provides a convenient way of getting the value of a key from a dictionary
without checking ahead of time whether the key exists, and without raising an error.
d.get(<key>) searches dictionary d for <key> and returns the associated value if it is found. If <key> is not
found, it returns None:
>>> print(d.get('b'))
20
>>> print(d.get('z'))
None
d.items()
Returns a list of key-value pairs in a dictionary.
d.items() returns a list of tuples containing the key-value pairs in d. The first item in each tuple is the key, and
the second item is the key’s value:
>>> list(d.items())
[('a', 10), ('b', 20), ('c', 30)]
>>> list(d.items())[1][0]
'b'
>>> list(d.items())[1][1]
20
d.keys()
Returns a list of keys in a dictionary.
>>> list(d.keys())
['a', 'b', 'c']
d.values()
Returns a list of values in a dictionary.
d.values() returns a list of all values in d:
>>> list(d.values())
[10, 20, 30]
d.update(<obj>)
Merges a dictionary with another dictionary or with an iterable of key-value pairs.
>>> d1.update(d2)
>>> d1
{'a': 10, 'b': 200, 'c': 30, 'd': 400}
Dictionary Methods
Methods Description
copy() They copy() method returns a shallow copy of the dictionary.
clear() The clear() method removes all items from the dictionary.
pop() Removes and returns an element from a dictionary having the given key.
popitem() Removes the arbitrary key-value pair from the dictionary and returns it as
tuple.
get() It is a conventional method to access a value for a key.
dictionary_name.values() returns a list of all the values available in a given dictionary.
str() Produces a printable string representation of a dictionary.
update() Adds dictionary dict2’s key-values pairs to dict
setdefault() Set dict[key]=default if key is not already in dict
keys() Returns list of dictionary dict’s keys
items() Returns a list of dict’s (key, value) tuple pairs
has_key() Returns true if key in dictionary dict, false otherwise
fromkeys() Create a new dictionary with keys from seq and values set to value.
type() Returns the type of the passed variable.
cmp() Compares elements of both dict.
Dictionary vs Lists
Dictionaries and lists share the following characteristics:
• Both are mutable.
• Both are dynamic. They can grow and shrink as needed.
• Both can be nested. A list can contain another list. A dictionary can contain another dictionary. A
dictionary can also contain a list, and vice versa.
• List elements are accessed by their position in the list, via indexing.
• Dictionary elements are accessed via keys.