DEV Community

Cover image for Learn Python #9 - Yeah Dictionary🐍
Nitin Reddy
Nitin Reddy

Posted on

Learn Python #9 - Yeah Dictionary🐍

Today we are going to deep dive into the main data structure in Python known as Dictionary.

Not a dictionary of words

What is Dictionary?💡

In Python, a dictionary is a list of key-value pairs.
Ok, let's take an example.

Creating a Dictionary💡

dictionary_1 = {
  'name': 'Nitin',
  'age': 34,
  'country': 'India'
}
Enter fullscreen mode Exit fullscreen mode

In the above example
name, age, and country are the keys with the corresponding values as Nitin, 34, and India.

Also, we can create a nested dictionary

dictionary_1 = {
  'name': 'Nitin',
  'age': 34,
  'address': {
    'street': 'Keshav'
    'country': 'India'
  }
}
Enter fullscreen mode Exit fullscreen mode

Easy to understand?💡

A dictionary contains

  • unique keys
  • key can have different types of values like number, string, boolean etc.

This would look similar to an object in JavaScript

Accessing a key's value💡

dictionary_1 = {
  'name': 'Nitin',
  'age': 34,
  'country': 'India'
}

dictionary_1['name']
// Nitin


dictionary_2 = {
  'name': 'Nitin',
  'age': 34,
  'address': {
    'street': 'Keshav'
    'country': 'India'
  }
}

dictionary_2['address']['street']
// Keshav
Enter fullscreen mode Exit fullscreen mode

To access any value in a dictionary, we need to enclose the key in square brackets []

think

Conclusion 📗

There are more posts on the way related to the tuples...So keep reading.

Thanks in advance for reading this article...🚀

I am more than happy to connect with you on

You can also find me on

Top comments (0)