Dictionary Data Type


Dictionary is an ordered set in a key:value pair of elements which are separated by comma (,) enclosed in curly braces {} and values can be assigned and accessed using square brackets [].

A key can hold any primitive data type whereas value is an arbitrary python object.

Example

        dict = {1:’Tutorials’, ‘two’:’Duniya’}
        print(dict[1])			    # prints value of element whose key is ‘1’
        print(dict[‘two’])		    # prints value of element whose key is ‘two’
        print(dict)			    # prints all the key:value pairs in the dictionary
        print(dict.keys())		    # prints all the keys in the dictionary
        print(dict.values())            # prints all the values in the dictionary        


Output:

        Tutorials
        Duniya
        {1:’Tutorials’, ‘two’:’Duniya’}
        [1, ‘two’]
        [‘Tutorials’, ‘Duniya’]