Python Dictionaries

Dictionaries

     A dictionary is unordered set of key:value pairs, with unique keys enclosed within {} brackets.
     It can be initialized as
d1 = {}
d2 = dict()
d3 = dict(key1=value1, key2=value2,…,keyN=valueM)
d4 = dict(some_iterable)
d5=dict(mapping_object)
There are following methods dictionary object has:-

dict.clear()

     Removes all items from the dictionary and return None.

dict.copy()

     Returns shallow copy of dictionary.

dict.fromkeys(k [,v])

     Creates new dictionary with keys from k and optionally taking values equal to v, and v Defaults to None.

dict.get(k, d)

     Returns value from dictionary whose key equal to k, else returns d, and d Defaults to None.

dict.has_key(k)

     Returns true if dictionary has key k else False.
NOTE: It is Removed From 3.x Version of Python

dict.items()

     Returns the list of key,value pair from dictionary as 2-tuples.

dict.iteritems()

     Returns the iterator over items of dictionary.

dict.iterkeys()

     Returns the iterator over keys of dictionary.

dict.itervalues()

     Returns the iterator over values of dictionary.

dict.keys()

     Returns the list of keys from dictionary.

dict.pop(k[,d])

     Returns the value of given key by removing key and associated value, if d is given optionally and key is not present d is returned else KeyError raised.

dict.popitem()

     Removes and Returns last added item i.e. (key, value) pair as tuple but raises KeyError if dictionary is empty.

dict.setdefault(k[, d])

     sets default value of key k if key k is not in dictionary.

dict.update(another_dictionary)

     It updates dictionary with items of another dictionary and Returns None. If key is present then it's value is updated with another dictionaries value at that key. As in words of The Python Founder Gudo Van Rossum, following operations are performed for d.update(E[, F]),
If E present and has a .keys() method, does: for k in E: D[k] = E[k]
If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v
In either case, this is followed by: for k in F: D[k] = F[k]

dict.values()

     Returns the list of values from dictionary.

dict.viewitems()

     Returns the unique set like object of items from dictionary.

dict.viewkeys()

     Returns the unique set like object of keys from dictionary.

dict.viewvalues()

     Returns of object with values from dictionary.
Let's understand this by example code

>>> d={'one':1, 'three':3, 'nine':'9', 'jack':'jill'}
>>> d['five']=5
>>> d
{'five': 5, 'nine': '9', 'three': 3, 'jack': 'jill', 'one': 1}
>>> dict1=d.copy()
>>> dict1
{'nine': '9', 'one': 1, 'five': 5, 'three': 3, 'jack': 'jill'}
>>> dict2=d.fromkeys('one')
>>> dict
{'e': None, 'o': None, 'n': None}
>>> dict2.clear()
>>> dict2
{}
>>> d.get('three')
3
>>> d.has_key('nine')
True
>>> d.items()
[('five', 5), ('nine', '9'), ('three', 3), ('jack', 'jill'), ('one', 1)]
>>> print d.iteritems()

>>> for k, v in d.iteritems():
...     print k, v
... 
five 5
nine 9
three 3
jack jill
one 1
>>> print d.iterkeys()

>>> for k in d.iterkeys():
...     print k
... 
five
nine
three
jack
one
>>> print d.itervalues()

>>> for v in d.itervalues():
...     print v
... 
5
9
3
jill
1
>>> print d.keys()
['five', 'nine', 'three', 'jack', 'one']
>>> d.pop('jack')
'jill'
>>> d
{'five': 5, 'nine': '9', 'three': 3, 'one': 1}
>>> d.popitem()
('five', 5)
>>> d
{'nine': '9', 'three': 3, 'one': 1}
>>> d.update(dict1)
>>> d
{'three': 3, 'one': 1, 'nine': '9', 'jack': 'jill', 'five': 5}
>>> print d.values()
[3, 1, '9', 'jill', 5]
>>> print d.viewitems()
dict_items([('three', 3), ('one', 1), ('nine', '9'), ('jack', 'jill'), ('five', 5)])
>>> d.viewkeys()
dict_keys(['three', 'one', 'nine', 'jack', 'five'])
>>> d.viewvalues()
dict_values([3, 1, '9', 'jill', 5])

Keep Following, For More.

Comments

Popular posts from this blog

Getting Started With Python

Python List

The dir Function