DEV Community

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

Posted on

1

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️