Dictionary & Its Functions Explained

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

Starting with the basic definition of Python Dictionary:

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

Now coming to the more formal approach:

Every programming language has its own distinct features, commonly known as its key features. That said, Python’s one out of box feature is “dictionaries”. Dictionaries may look very similar to a “List”, but dictionaries have some distinct key features that a list do not hold, and those features are what makes it special. Moving on to the features:

  • 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 cointains key-value pairs and indexing is done with keys. Also after the 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 override)

If we talk little about how it works, its syntax comprises of key and values separated by colons in between 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. And for the successful retrieval of the data, we must know the key, so we can access its value. Like in the regular oxford dictionary, if we do not know the word or its spelling, we cannot obtain its definition. 

With the help of dictionaries, we do not have to do most of our work manually through code like in C or C#. Python along with dictionary provides us with a long list of already defined methods that can help us do our work in a shorter span of time with only a little bit of code. Some of these methods include, clear(), copy(), popitem(), etc. No extra effort must be put in order to learn these methods 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, etc.

Some distinct features that a dictionary provides are:

  • We can store heterogeneous data into our dictionary i.e. numbers, strings, tuples, and other objects.
  • Different data types can be used in a single list.

This is the basic explanation of Dictionaries in Python. You will get further details along with explanations and implementation in the video tutorial.

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",
      "Rohan":"Fish",
      "SkillF":"Roti",
      "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())

Comments(5)

kunwarrahul 11 months ago
thnks bro for providing us python tutorials
anshul860 6 months, 3 weeks 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"} Print(Dic1) I am unable to run this it's showing error
Shashu12 2 months, 3 weeks ago
broo plss watch the python tutorial once again.and try karoo
medhansh123 3 weeks, 6 days ago
thank you sir for providing free python courses and materials
Vivek-Mishra 1 week, 6 days ago
Full Notes and Source Code ::::::::::} https://github.com/Optimized-World/Complete-Python-Tutorial-and-Notes |::::::::::::| Thanks, Harry Bhai for Awesome Tutorials

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here

Course Content