Python Dictionary

A simple definition of a python dictionary is a collection of data in key pair values. Generally, the key is the first value of the dictionary which carry the data. Additionally, in the dictionary, the key always needed to be unique we can not able to define multiple keys in a single dictionary. With a help of key, all operation has performed on the dictionary. In this article, you will learn the python dictionary from basic to advance.

What is a python dictionary?

python dictionary is a collection that allowed to put data with help of a key. In addition, each key is unique and all the operations perform base on the key. The simple syntax of the dictionary is given below.

variable={‘key’: ‘value’}

Looking inside python dictionary

Unlike other data structures, we can access data from dictionaries using a key. The key value is always unique, if you use the name of the key that does not exist in the dictionary then you will be seen the key error. The following code illustrates how can we create a dictionary and also how we access data from the dictionary.

dictionary={'key1':'value1','key2':'value2'}
dictionary['key1']
output:
value1
key error

How to find keys name from the dictionary

we can access the name of a key by using a loop following code to show how can we get keys.

dictionary={'key1':'value1','key2':'value2'}
for keys, values in dictionary.items():
    print(keys)
Output:
key1
key2

We can also check the key whether it is present in a dictionary or not, so the following code use for checking the key in a dictionary.

dictionary={'key1':'value1','key2':'value2'}
if "key1" in dictionary: 
    print("Yes, 'key1' is one of the keys in the dictionary")
else:
    print("No, key isnot exist in above dictionary")
Output:
Yes, 'key1' is one of the keys in the dictionary

Adding and Removing item in a dictionary

There are tremendous ways to add and remove data in a dictionary generally, for adding items in a dictionary we need to define new index key and their respective value. The following code depicts how to add items to a dictionary.

In the case of deleting, we have several functions available, but in this course, we use the following method for deleting items from a dictionary.

  • The pop() method removes the item with the specified key name
  • The popitem() method removes the last inserted item
  • The del keyword removes the item with the specified key name
  • The clear() method empties the dictionary

The following code shows the use of this function one by one.

dictionary={'key1':'value1','key2':'value2','key3':'Value3','key4':'value4'}
print(dictionary)
dictionary.pop('key1')
print(dictionary)
dictionary.popitem()
print(dictionary)
del dictionary['key2']
print(dictionary)
Output:
{'key1': 'value1', 'key2': 'value2', 'key3': 'Value3', 'key4': 'value4'}
{'key2': 'value2', 'key3': 'Value3', 'key4': 'value4'}
{'key2': 'value2', 'key3': 'Value3'}
{'key3': 'Value3'}

Copy a Dictionary

You cannot simply copy the dictionary eg dict2= dict1 because any change that you made in a dict1 will appear in dict2 so that we need to use the copy() function for creating a duplicate dictionary. The following code shows how to copy a dictionary.

 thisdict =	{
  "name": "Return Script",
  "year": 2020
}
mydict1 = thisdict.copy()
print(mydict1)
mydict2=dict(thisdict)
print(mydict2)
Output:
{'name': 'Return Script', 'year': 2020}
{'name': 'Return Script', 'year': 2020}

Nested Dictionary and the dict() Constructor

The term Nested refers to that a single dictionary that has another dictionary. Additionally, a nested dictionary is one of the most useful applications of a dictionary because we can create a dictionary inside a dictionary. Further, for adding items in a nested dictionary we need to call a constructor which is called a dict() constructor. The following code is used to create a nested dictionary and the use of dict() constructor.

myfamily = {
  "child1" : {
    "name" : "Ram",
    "year" : 2004
  },
  "child2" : {
    "name" : "Sita",
    "year" : 2007
  },
  "child3" : {
    "name" : "Gita",
    "year" : 2011
  }
}
print(myfamily)
thisdict = dict(brand="ganesh", year=1964)
print(thisdict)
Output:
{'child1': {'name': 'Ram', 'year': 2004}, 'child2': {'name': 'Sita', 'year': 2007}, 'child3': {'name': 'Gita', 'year': 2011}}
{'brand': 'ganesh', 'year': 1964}

Conclusion

OK, this is the end of the article I hope you can get a good lesson from what I deliver in this article. I ask forgiveness for any word and behave which are not to be. Thank you for your kind and attention guys. Stay tuned for the next article. if you are searching for a free python course here is a link. If you have any questions regarding this article please feel free to comment below.

Leave a Reply