Python Dictionary

                           Dictionary

A dicitonary is a key-value pair collection which is unordered and can be changed or altered. We can not perform indexing on dictionary like lists but dictionary is still indexed. How?
Well, we can use key inplace of index to get the value.
We use curly braces { } to represent the dictionary. 
E.g. dict1 = {'a':1, 'b':2, 'c':3, 'd':4}

The value on left side of colon is called key, and value on right side of colon is called value. Hence key-value pair. We can use any datatype for keys and values. We can also use lists, sets, dictionary,etc.

To access the value, we use keys. We can also use get() function for access the value. Just put the key in get() and value is accessed.
E.g.  dict1['a']          # output : 1
    dict1['c']          # output : 3
    dict1.get('b')      # output : 2

The value can be changed in dictionary. 
E.g. dict1['a'] = 10 
    print(dict1)         # output : {'a':10, 'b':2, 'c':3, 'd':4}
Now using same method above we can also add the new key-value pair in dictionary. If the key is present then the value is updated else new key-value pair is added in dictionary.
E.g.  dict1['z'] = 26        
    print(dict1)     # output: {'a':10, 'b':2, 'c':3, 'd':4, 'z':26}

items() function is used to get the list of all key-value pair present in the dicitonary.
E.g. dict1.items()     # output : dict_items([('a', 10), ('b', 2),                                      ('c', 3), ('d', 4), ('z', 26)])

values() function is used to get the list of all values present in dictionary.
E.g. dict1.values()     # output : dict_values([10, 2, 3, 4, 26])

keys() function is used to get the list of all keys present in dictionary.
E.g. dict1.keys()  # output : dict_keys(['a', 'b', 'c', 'd', 'z'])

len() function is used to get the length of dictinary.
E.g. len(dict1)     # output : 5

del keyword can be used to delete the particular item or the whole dicitonary.
E.g.  del dict1['a']  
    print(dict1)       # output : {'b': 2, 'c': 3, 'd': 4, 'z': 26}
    del dict1 
    print(dict1)          # Error : dict1 not defined

pop() method can also be used to delete the item.
E.g.  dict2 = {1:1, 2:4, 3:9, 4:16}
    dict2.pop(3)
    print(dict2)    # output :   {1: 1, 2: 4, 4: 16}

popitem() function is used to remove the item which is added lastly and it also return the removed item.
E.g. dict2.popitem()           # output : (4: 16)

clear() function is used to clear the dictionary.

dict() constructor can be used to define the dicitnary.
E.g.  dict3 = dict(a=1, b=2, c=3)
    print(dict3)      #output : {'a': 1, 'b': 2, 'c': 3}
Remeber that we use = inplace of colon and in key values we do not use string literal. Empty curly braces can also be used to define the empty dicitonary. 

Dictionaries can be iterated, we can check the presence of item using key. In data structure we have concept of Hash Mapping, we can performed that very easily using Python Dictionary. Dictioanry is a cool data structure in Python!

Comments

Popular posts from this blog

Loops in Python

Python OOPs

Regular Expressions