Course Content

Dictionary & Its Functions Explained

Before going through the actual content i.e. the implementation of a dictionary, it is important to know some basic theories so that we can know what we are going to learn and why we are spending our precious time learning it.

Let's start with the basic definition of a Python Dictionary:

“Python dictionary is an unordered collection of items. Each item of the dictionary has a key and value pair/ key-value pair.”

Now coming to the more formal approach:

Every programming language has its own distinct features, commonly known as its key features. With that said, Python’s one out of the box feature is “dictionaries”. Dictionaries may look very similar to a “List”, but dictionaries have some distinct features that do not hold true for other data types like lists, and those features make it (python dictionary) special.

Here are a few important features of a python dictionary:

  • It is unordered (no sequence is required - data or entries have no order)
  • It is mutable (values can be changed even after its formation or new data/information can be added to the already existing dictionary, we can also pop/remove an entry completely)
  • It is indexed (Dictionary contains key-value pairs and indexing is done with keys. Also after the Python 3.7th update the compiler stores the entries in the order they are created)
  • No duplication of data (each key is unique; no two keys can have the same name so there is no chance for a data being overriden)

If we talk a little about how it works, its syntax comprises of key and values separated by colons in curly brackets, where the key is used as a keyword, as we see in real life dictionaries, and the values are like the explanation of the key or what the key holds (the value). And for the successful retrieval of the data, we must know the key, so that we can access its value just like in a regular oxford dictionary where if we don't know the word or its spelling, we cannot obtain its definition. Let's look into the syntax of a Python dictionary

a = {'key', 'value', 'cow':'mooh'}
#will print "mooh" on the screen

With the help of dictionaries, we do not have to do most of our work manually through code like in C or C++. What I mean by this is that Python provides us with a long list of already defined methods for dictionaries that can help us to do our work in a shorter span of time with a very little amount of code. Some of these methods are, clear(), copy(), popitem(), etc. The best part about them is that no extra effort is required to be put in order to learn the functionality as their names explain their functions (in most of the cases), such as clear() will clear all the data from the dictionary, making it empty, copy() will make a copy of the dictionary, etc.

Some distinct features that a dictionary provides are:

  • We can store heterogeneous data into our dictionary i.e. numbers, strings, tuples, and the other objects can be stored in the same dictionary.
  • Different data types can be used in a single list, which can be made the value of some keys in the dictionary. etc.

This was pretty much about dictionaries in Python. You will get further details along with explanations and implementation in the video tutorial.

I hope you are enjoying it. Do not forget subscribing to CodeWithHarry.

Code file as described in the video

# Dictionary is nothing but key value pairs
d1 = {}
# print(type(d1))
d2 = {"Harry":"Burger",
      "Shubham":{"B":"maggie", "L":"roti", "D":"Chicken"}}
# d2["Ankit"] = "Junk Food"
# d2[420] = "Kebabs"
# print(d2)
# del d2[420]
# print(d2["Shubham"])
# d3 = d2.copy()
# del d3["Harry"]
# d2.update({"Leena":"Toffee"})
# print(d2.keys())
# print(d2.items())


kunwarrahul 1 year, 2 months ago
thnks bro for providing us python tutorials
anshul860 10 months, 1 week ago
print("Select any word to get its meaning\n", "1) Abandoned\n", "2) Plethora\n", "3) Imbibe\n", "4) Inculcate\n")
Dic1 = {"Abandoned":{"deserted", "forsaken", "cast aside/off", "jilted", "stranded", "rejected"},
        "Plethora":"excess", "abundance", "overabundance", "superfluity", "surfeit",
        "Imbibe": "drink", "consume", "sup", "sip", "quaff", "swallow", "down", "guzzle",
        "Inculcate": "instil", "implant", "fix", "ingrain", "infuse", "impress", "imprint"}

I am unable to run this
it's showing error
Shashu12 6 months, 1 week ago
broo plss watch the python tutorial once again.and try karoo
vish09 1 month, 3 weeks ago
Print kike ho one side another side print like ho
vish09 1 month, 3 weeks ago
Pehle line me aap vo dictionary ke andar dictionary leeke he lagta he vo list or tuple hona chahiye qnki dictionary ke andar dictionary ese nahi banate he
medhansh123 4 months, 2 weeks ago
thank you sir for providing free python courses and materials
Vivek-Mishra 4 months ago
Full Notes and Source Code ::::::::::} https://github.com/Optimized-World/Complete-Python-Tutorial-and-Notes |::::::::::::| Thanks, Harry Bhai for Awesome Tutorials
sahil1211 2 months ago
hey . 
how can i add the key and value inside the dictionary named shubham (in your example ). 
ankit100 1 month, 4 weeks ago
thank you sir
vish09 1 month, 3 weeks ago
#take input from user
print("input any of three vishnu/house/mon")



Course Announcements

Any Course related announcements will be posted here