Free Web Development Online Tutorials, Learn to Code
Python dictionary methods example, python dictionary append value to key, Python dictionary methods

Python dictionary methods example

In this article you will learn how to work with Python dictionary object.

Python dictionary functions tutorial

Python Dictionary is a collection type object, which can store data using “key:value”pair

We can create python dictionary object different ways, here are few examples.

    
class DictionaryExample(object):
   
    def __init__(self):
         self.Title = "Dictionary Examples";
    
    def showFunction(obj):
        Dict = {"Course:Python"} 
        print("New Dictionary created") 
        print(Dict) 
     

We can create dictionary object with different type of keys, but we must make sure providing right key at the time of retrieval

Dict = {'Course' : 'Python', 10: [14,4.5,.60], 'Provider' : 'WebTrainingRoom'} 
print(Dict)

We also can create dictionary with dict() method

Dict = dict({1: 'Red', 2: 'Green', 3:'Blue'}) 
print(Dict) 

Python Dictionary Methods

Here are some python dictionary methods with examples

  1. clear(): Removes all the elements from the dictionary, make it empty
    Dict = dict({1: 'Red', 2: 'Green', 3:'Blue'}) 
    Dict.clear(); 
    print(Dict) 
    
  2. copy() : Copy a dictionary object with data to a new dictionary object
    d1 = dict({1: 'Red', 2: 'Green', 3:'Blue'}) 
    d2=d1.copy(); 
    print(d2) 
    
  3. keys() : Get all keys of a dictionary object, useful when you want to retrieve value by using a key
    d1 = dict({1: 'Red', 2: 'Green', 3:'Blue'}) 
    allkeys=d1.keys(); 
    
  4. popitem() : Pick the last item from the dictionary
    d1 = dict({1: 'Red', 2: 'Green', 3:'Blue'}) 
    d2=d1.popitem(); 
    print(d2);
    #result : (3, 'Blue')
    
  5. pop() : Pick the Specified item based on given key from the dictionary, , if no matching key found then it throws error
    d1 = dict({1: 'Red', 2: 'Green', 3:'Blue'}) 
    d2=d1.pop(2); 
    print(d2);
    #result : ('Green')
    
  6. update() : Insert an item to the dictionary object; update the dictionary with new item
    d1 = dict({1: 'Red', 2: 'Green', 3:'Blue'}) 
    d1.update({"color": "Orange"}); 
    print(d1) 
    #result : {1: 'Red', 2: 'Green', 3:'Blue', "color": "Orange"} 
    
  7. values() : Get all values of a dictionary object, useful when you want to retrieve all values
    d1 = dict({1: 'Red', 2: 'Green', 3:'Blue'}) 
    allValues=d1.values(); 
    
  8. sort() : There is no direct method for sorting a dictionary collection in python, but we can make a list object of all keys in dictionary, then we can sort the key-list , here is an example of how to sort a dictionary object in python
    Dict  = dict({"India": 'Delhi', "Uk": 'London', "USA":'Washington'})         
    lst1=list(Dict.keys());
    lst1.sort();
    for K in lst1:
        print(":".join((K,str(Dict[K]))));
    
  9. len() : this method is to find the length of a dictionary collection in python
    Dict  = dict({"India": 'Delhi', "Uk": 'London', "USA":'Washington'}) 
    print("Length : %d" % len (Dict))
    
  10. str(): this will form a string from given dictionary object.
    Dict  = dict({"India": 'Delhi', "Uk": 'London', "USA":'Washington'}) 
    print(str(Dict))
    
 


Python dictionary

Python dictionary functions
Python programming examples | Join Python Course