Advertisement



< Prev
Next >



Python - Dictionary





In our last few Python article, we have explained the collection data types such as - tuple and list. Moving on in this tutorial, we are going to explain another collection data type - dictionary, which is used to store items in the form of key:value pairs, where both key and value are objects.

As of Python 3.7.0 version, dictionary is an ordered sequence of items, in which the items are stored in the order in which they are declared i.e. the insertion order is preserved.

Note : Only the immutable data types such as int, float, tuple, str can be used as a key, where value can be any Python data type.





# Python - Example with methods of dictionary



# First dictionary
dict1 = {
    4:'Fourth',
    3:'Third',
    'two':'Second',
    'one':'First',
    5:'Fifth'
    }


# Second dictionary 
dict2 = {
    40:'Forty',
    30:'Thirty'
    }

	
	
# Original dictionary 
print('Original dictionary : ', dict1)


# Calling pop(key) method to remove a key and its associated value
dict1.pop(4)


# Updated dictionary 
print('Updated dictionary : ', dict1)



# Calling pop(key,value) method to remove a key and its associated value
dict1.pop('two', '2nd')


# Updated dictionary 
print('Updated dictionary : ', dict1)



# Calling popitem() method to pop an arbitrary item
dict1.popitem()


# Updated dictionary 
print('Updated dictionary : ', dict1)



# Printing the values in a dictionary
print('Values in dictionary : ', dict1.values())


Output


Original dictionary :  {4: 'Fourth', 3: 'Third', 'two': 'Second', 'one': 'First', 5: 'Fifth'}
Updated dictionary :  {3: 'Third', 'two': 'Second', 'one': 'First', 5: 'Fifth'}
Updated dictionary :  {3: 'Third', 'one': 'First', 5: 'Fifth'}
Updated dictionary :  {3: 'Third', 'one': 'First'}
Values in dictionary :  dict_values(['Third', 'First'])






  • Some important built-in functions to work with dictionary


  • Functions Description
    max() Returns the maximum value of keys in a dictionary.
    min() Returns the minimum value of keys in a dictionary.
    len() Returs the total length of a dictionary.
    sum() Returns sum of all keys in a dictionary.
    sorted() Returns a sorted copy of a dictionary.
    any() Returns true if any key in a dictionary is true, else false.
    all() Returns true if all key in a dictionary are true, else false.




  • Example of using built-in functions with dictionary


  • #Python - Built-in functions to work with dictionary
    
    
    
    # First dictionary
    dict1 = {
        4:'Fourth',
        3:'Third',
        2:'Second',
        1:'First',
        5:'Fifth'
        }
    
    
    # Second dictionary 
    dict2 = {
        40:'Forty',
        True:'Yes',
        False:'No'
        }
    
    # 1 is boolean true, 0  for boolean false
    # True(with capital T) is boolean true, False(with capital F) is boolean false
    
     
    #Printing dictionarys
    print('Dictionary1 : ', dict1)
    print('Dictionary2 : ', dict2)
    
    
    
    #Using max() function with a dictionary.
    print('Maximum key in dict1 : ', max(dict1))
    
    
    #Using min() function with a dictionary.
    print('Minimum key in dict1 : ', min(dict1))
    
    
    #Using sum() function with a dictionary.
    print('Sum of all keys in dict1 : ', sum(dict1))
    
    
    #Using sorted() function with a dictionary.
    print('Sorted copy of dict1 : ', sorted(dict1))
    
    
    #Using all() function with a dictionary.
    print('all() boolean true keys in dict2 : ', all(dict2))
    
    
    #Using any() function with a dictionary.
    print('any boolean true key in dict2 : ', any(dict2))


    Output


    Dictionary1 :  {4: 'Fourth', 3: 'Third', 2: 'Second', 1: 'First', 5: 'Fifth'}
    Dictionary2 :  {40: 'Forty', True: 'Yes', False: 'No'}
    Maximum key in dict1 :  5
    Minimum key in dict1 :  1
    Sum of all keys in dict1 :  15
    Sorted copy of dict1 :  [1, 2, 3, 4, 5]
    all() boolean true keys in dict2 :  False
    any boolean true key in dict2 :  True





    Please share this article -




    < Prev
    Next >
    < Python Set
    Python - String >



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement



    Notifications



    Please check our latest addition

    C#, PYTHON and DJANGO


    Advertisement