Dictionaries

Dictionaries in Python are used to store data in key-value pairs. They are very useful when you want to quickly look up a value using a unique key, rather than searching through a list. In data analysis, dictionaries are commonly used for mapping values, storing structured information, handling JSON data from APIs, and creating quick reference tables for cleaning or transforming datasets.

A dictionary is created using curly braces, with keys and values separated by a colon. For example, a dictionary can store a person’s details like name, age, and city. Keys are usually strings, but they can also be numbers or other immutable types. Values can be anything: numbers, strings, lists, even other dictionaries. This makes dictionaries flexible for handling real-world data.

The main benefit of dictionaries is fast access. If you know the key, you can directly get the value without checking every item. This is helpful for tasks like category mapping, where you convert short codes into full names, or standardise inconsistent labels. For example, you can map “MH” to “Maharashtra” or “HR” to “Haryana” using a dictionary.

You can add new key-value pairs, update existing values, or remove entries. Common methods include keys() to view all keys, values() to view all values, and items() to view key-value pairs together. The get() method is useful because it allows you to access a key safely without causing an error if the key is missing. This is important when dealing with messy data or API responses where some fields may not exist.

Dictionaries also support nested structures, where a value can be another dictionary. This is very common in JSON data, where you may have multiple levels of information. Learning dictionaries helps you work smoothly with structured data, create efficient transformations, and write cleaner code for real analysis tasks.

Lists
Sets

Get industry recognized certification – Contact us

keyboard_arrow_up