Python Dictionary Methods

python-dictionary-methods-ipcisco.com

1Like python lists and sets, python dictionaries uses also different methods. In this lesson we will focus on these python dictionary methods. We will give different examples for each method and after this lesson, you will be ready to use dictionary methods.

 

So, which methods are used by dictionaries? These methods are given below:

 

  • get()
  • keys()
  • values()
  • items()
  • pop()
  • popitem()
  • setdefault()
  • update()
  • clear()
  • copy()
  • fromkeys()

 

Now, let’s focus these python dictionary methods and learn them one by one.

 


 

Using Methods With Dictionaries

 

Python Dictionary Methods: get() method

 

get() method is used to get the value of a given key. As you know there are key:value pair in python dictionaries. With this method, we give the key and receive its value.

 

device ={
  "vendor": "Cisco",
  "model": "9000 series",
  "RU": 44
}
x = device.get("vendor")
print(x)
Cisco

python-dictionary-methods-get-method

 

The above key pair exist already. We can use this method to receivea new defined key value like below:

 

device ={
  "vendor": "Cisco",
  "model": "9000 series",
  "RU": 44
  }
x = device.get("port speed", 400)
print(x)

 

The output of this python code will be:

 

400

 

methods-used-by-python-dictionaries

 

keys() Method 

 

keys() method is used to receive the keys as output. AS you know dictionaries consist of key:value pairs. With keys() method, we receive the keys define in a dictionary.

 

device ={
  "vendor": "Cisco",
  "model": "9000 series",
  "RU": 44
}
x = device.keys()
print(x)

 

dict_keys(['vendor', 'model', 'RU'])

 

python-dictionary-methods-keys-method

 

We can also add a new item in a dictionary. And if we assign keys to a variable, and after we add a new item to the dictionary, then the variable will also change and includes this new item. BElow, there is an example for this usage.

 

device ={
  "vendor": "Cisco",
  "model": "9000 series",
  "RU": 44
}
x = device.keys()
device["port speed"] = "400G"
print(x)

 

dict_keys(['vendor', 'model', 'RU', 'port speed'])

 

values() Method

 

Like keys, we can also receive values as output with the help of values() method.

 

device ={
  "vendor": "Cisco",
  "model": "9000 series",
  "RU": 44
}
x = device.values()
print(x)
dict_values(['Cisco', '9000 series', 44])

 

python-dictionary-methods-values-method

 

Again if we add a new ite mor if we change the value of an item, and we assign the values to a variable with values() method, the variable will include new values.

device ={
  "vendor": "Cisco",
  "model": "9000 series",
  "RU": 44
}
x = device.values()
device["port speed"] = "400G"
print(x)

 

dict_values(['Cisco', '9000 series', 44, '400G'])

 

items() method

 

items() method gives all the key:value pairs. In other words, we get all items with this method. Here, be careful about the round and square pharantesis.

 

device ={
  "vendor": "Cisco",
  "model": "9000 series",
  "RU": 44
}
x = device.items()
print(x)

 

dict_items([('vendor', 'Cisco'), ('model', '9000 series'), ('RU', 44)])

 

python-dictionary-methods-items-method

 

Python Dictionary Methods: pop()  Method

 

pop() method is used to remove the specified item from the dictionary. If we assign this method to a variable, and if we give any key as parameter, it returns as the deleted value of this key. After this process, if we print the updated dictionary, we do not see this item in the python dicitonary.

 

device ={
  "vendor": "Cisco",
  "model": "9000 series",
  "RU": 44
}
device.pop("model")
print(device)

 

{'vendor': 'Cisco', 'RU': 44}

 

pop-method-of-python-dictionaries

 

device ={
  "vendor": "Cisco",
  "model": "9000 series",
  "RU": 44
}
x = device.pop("model")
print(x)

 

The output will be :

 

9000 series

 

popitem() Method

 

popitem() method is used to remove the last item in the dictionary. This is a method like pop method used with list and tuples. It is not similar with the pop method used with dictionaries.

 

device ={
  "vendor": "Nokia",
  "model": "7950 XRS",
  "RU": 39
}
device.popitem()
print(device)
{'vendor': 'Nokia', 'model': '7950 XRS'}

 

Again, if we asign this method to a variable, the deleted item is assigned to the variable.

 

device ={
  "vendor": "Nokia",
  "model": "7950 XRS",
  "RU": 39
}
x=device.popitem()
print(x)

 

As we mentined above, the return of popitem() method will ve the deleted item like below::

 

('RU', 39)

python-dictionary-methods-popitem-method

 

setdefault() Method

 

setdefault() method is used to return with the value of a key if it exist in the dictionary.

 

device ={
  "vendor": "Nokia",
  "model": "7950 XRS",
  "RU": 39
}
x = device.setdefault("model","7750 SR")
print(x)

  

7950 XRS

{'vendor': 'Nokia', 'model': '7950 XRS', 'RU': 39}

 

setdefault-method-of-python-dictionaries

 

If there is no items like it, then it ass this item and again returns with the value of the specified key.

 

device ={
  "vendor": "Nokia",
  "model": "7950 XRS",
  "RU": 39
}
x = device.setdefault("speed","400G")
print(x)
print(device)

 

400G
{'vendor': 'Nokia', 'model': '7950 XRS', 'RU': 39, 'speed': '400G'}

 

Python Dictionary Methods: update() Method

 

One of the other python dictionary methods is update method.  With update() method, we can insert new items to the dictionary.

 

device ={
  "vendor": "Nokia",
  "model": "7950 XRS",
  "RU": 39
}
device.update({"speed": "400G"})
print(device)

 

{'vendor': 'Nokia', 'model': '7950 XRS', 'RU': 39, 'speed': '400G'}

 

Python Dictionary Methods:  clear() Method

 

As in other data types that stores data collections, clear() method is used to delete all the items in the dictionary. In other words it empties the dictionary.

 

device ={
  "vendor": "Cisco",
  "model": "9000 series",
  "RU": 44
  }
device.clear()
print(device)

 

{}

 

clear-method-of-python-dictionaries

 

Python Dictionary Methods: copy() Method

 

With copy() method, we copy the dictionary and we can assign it to any variable or we can use it in any other place.

 

device ={
  "vendor": "Cisco",
  "model": "9000 series",
  "RU": 44
  }
x=device.copy()
print(x)

 

{'vendor': 'Cisco', 'model': '9000 series', 'RU': 44}

 

copy-method-of-python-dictionaries

fromkeys() Method

 

One of the other python dictionary methods is from keys() method. fromkeys() method is used to receive, specified keys and values.

 

vendors = ('Cisco', 'Nokia', 'Huawei')
x = dict.fromkeys(vendors)
print(x)

 

{'Cisco': None, 'Nokia': None, 'Huawei': None}

 

Below, we will use two parameters. The first parameter will give key and the second oen will give value. So, for eack key, we will have three values.

 

devices = ('Routers', 'Switches', 'Firewalls')
vendors = ('Cisco', 'Nokia', 'Huawei')
x = dict.fromkeys(devices,vendors)
print(x)

 

{'Routers': ('Cisco', 'Nokia', 'Huawei'), 'Switches': ('Cisco', 'Nokia', 'Huawei'), 'Firewalls': ('Cisco', 'Nokia', 'Huawei')}

 

In this lesson, we have learned different python dictionary methods. You can do more practice on each of these dictionary methods. With more practice, you will be more ready to use these methods in your coding examples.

Back to: Python Programming Course > Python Dictionaries

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact

  • info@ipcisco.com
IPCisco is the Winner! “Best Certification Study Journey of 2019!”

Cisco-ITBlogAwards-2019-Winner-IPCisco-k