VOOZH about

URL: https://www.analyticsvidhya.com/blog/2021/04/understand-the-concept-of-dictionary/

⇱ Dictionary | Python Data Structures - Dictionaries | What is a dictionary


India's Most Futuristic AI Conference Is Back – Bigger, Sharper, Bolder

  • d
  • :
  • h
  • :
  • m
  • :
  • s

Understand the Concept of Dictionary

Shipra Saxena Last Updated : 24 Oct, 2024
4 min read

Objective

  • Dictionary is another composite data type provided by python
  • Understand the concept of a dictionary and the basic operations performed on it

Note: If you are more interested in learning concepts in an Audio-Visual format, We have this entire article explained in the video below. If not, you may continue reading.

Introduction

Let’s start with a scenario. Suppose, we have stored the marks of a student sam in a list.

Can you tell, after looking at this list how much he has scored in History or Geography? No, you can not until you have the data where you have stored which subject marks are stored at which index.

If we have this data, you can easily answer that. Since History stored at index 1, we can say that Sam scored 36 marks in History.

But there are a few problems with this approach.

  • Many a time this data is not available or hard to create.
  • Sometimes we do not want our data to have a sequence. Like in the previous example it doesn’t matter History marks are stored after Geography or before it.
  • Also, sometimes we want to access our data by names as in the example above. For instance, finding the History marks using the word ” History” not index.

All these issues can be solved using a Python data structure called Dictionary. This is how the dictionary is going to solve our problem.

πŸ‘ dictionary data

Instead of indices, the marks are stored using names or keys in the dictionaries. Here, the subject names Geography, History, Mathematics are the keys, and the marks 45, 36,54, etc. are the values corresponding to these keys.

What is a Dictionary?

A dictionary is an unordered data structure with elements separated by a comma and stored as a key-value pair. It is enclosed within curly brackets. Also, the key-value pair is separated by a : (colon).

Here is one example of. Here, the key is the name of the person and the value is a number.

We can also have any other data structure as a value. Look at the example below, here we have a list of numbers as value for each key.

Accessing Elements of a Dictionary

Now let’s see how we can access elements of a dictionary. Elements are accessed by keys rather than index. If we try to access a dictionary using the index, we get an error. Because this is not elements are stored inside it.

dict2 = {'Ramesh':[150,46],'Suresh':[146,58],'Sudesh':[160,50]}

dict2[1]

Elements are stored using keys. So to access them we need to use the key. Suppose you want to access the details of the key Suresh use  dict2[β€˜Suresh] as shown in the following image.

πŸ‘ Accessing elements

It will return the values stored using the key Suresh i.e a list of values 146 and 58.

Adding Elements to a Dictionary

Now let’s move on to see how to add elements to a dictionary. To add a single value to the dictionary we can use the name of the dictionary along with the new key enclosed in a pair of square brackets. See the image below.

When you print the dictionary you can see a new key-value pair added to the dictionary.

What if we want to add multiple elements to a dictionary. We can do this using the update function. This update function takes it as an input.

In our example, we are adding to key-value pair  Sunil and Disha to the existing dictionary using the update function. When we do so the two new elements get added to the dictionary.

This was all about how we can add elements to dictionaries.

Deleting Elements from a dictionary

What if you want to delete an element? Deleting an element from a dictionary is very much similar to that of a list. The only difference is instead of the index we pass the key within the square bracket that you want to delete from it.

For example, if I want to delete the details of Ramesh. We can do del dict2[β€˜Ramesh] as shown below.

It will remove the Ramesh key-value pair from the dictionary. After performing the deletion, it will have only two elements.

End Notes

This was all about dictionaries in this article. We saw how easily we can store and access the data in dictionaries. It uses the key-value pair format to store the values and allow instant access without any overhead. We also saw how to add and remove the elements from an existing dictionary.

Some important points about dictionaries-

  • Dictionaries are mutable.
  • They are dynamic which means they can grow and shrink according to the requirement.
  • Dictionaries can be nested.

If you are looking to kick start your Data Science Journey and want every topic under one roof, your search stops here. Check out Analytics Vidhya’s Certified AI & ML BlackBelt Plus Program

If you have any queries let me know in the comment section!

Shipra is a Data Science enthusiast, Exploring Machine learning and Deep learning algorithms. She is also interested in Big data technologies. She believes learning is a continuous process so keep moving.

Login to continue reading and enjoy expert-curated content.

Free Courses

Generative AI - A Way of Life

Explore Generative AI for beginners: create text and images, use top AI tools, learn practical skills, and ethics.

Getting Started with Large Language Models

Master Large Language Models (LLMs) with this course, offering clear guidance in NLP and model training made simple.

Building LLM Applications using Prompt Engineering

This free course guides you on building LLM apps, mastering prompt engineering, and developing chatbots with enterprise data.

Improving Real World RAG Systems: Key Challenges & Practical Solutions

Explore practical solutions, advanced retrieval strategies, and agentic RAG systems to improve context, relevance, and accuracy in AI-driven applications.

Microsoft Excel: Formulas & Functions

Master MS Excel for data analysis with key formulas, functions, and LookUp tools in this comprehensive course.

Responses From Readers

Shahnawaz Sayyed

Very good explanation for beginner's...Its so simple and crisp.. Thanks for sharing

Devil deva

Sir could you please explain more stuff about dictionary . Cus I want to more about this please update it

SoundofText

Great blog post! I really like the explanation of how dictionaries work in Python, and how they can be used to store and manipulate data. It's helpful to have a clear understanding of what a dictionary is and how it differs from other data structures, like lists. Thanks for sharing!

Flagship Programs

GenAI Pinnacle Program| GenAI Pinnacle Plus Program| AI/ML BlackBelt Program| Agentic AI Pioneer Program

Free Courses

Generative AI| DeepSeek| OpenAI Agent SDK| LLM Applications using Prompt Engineering| DeepSeek from Scratch| Stability.AI| SSM & MAMBA| RAG Systems using LlamaIndex| Building LLMs for Code| Python| Microsoft Excel| Machine Learning| Deep Learning| Mastering Multimodal RAG| Introduction to Transformer Model| Bagging & Boosting| Loan Prediction| Time Series Forecasting| Tableau| Business Analytics| Vibe Coding in Windsurf| Model Deployment using FastAPI| Building Data Analyst AI Agent| Getting started with OpenAI o3-mini| Introduction to Transformers and Attention Mechanisms

Popular Categories

AI Agents| Generative AI| Prompt Engineering| Generative AI Application| News| Technical Guides| AI Tools| Interview Preparation| Research Papers| Success Stories| Quiz| Use Cases| Listicles

Generative AI Tools and Techniques

GANs| VAEs| Transformers| StyleGAN| Pix2Pix| Autoencoders| GPT| BERT| Word2Vec| LSTM| Attention Mechanisms| Diffusion Models| LLMs| SLMs| Encoder Decoder Models| Prompt Engineering| LangChain| LlamaIndex| RAG| Fine-tuning| LangChain AI Agent| Multimodal Models| RNNs| DCGAN| ProGAN| Text-to-Image Models| DDPM| Document Question Answering| Imagen| T5 (Text-to-Text Transfer Transformer)| Seq2seq Models| WaveNet| Attention Is All You Need (Transformer Architecture) | WindSurf| Cursor

Popular GenAI Models

Llama 4| Llama 3.1| GPT 4.5| GPT 4.1| GPT 4o| o3-mini| Sora| DeepSeek R1| DeepSeek V3| Janus Pro| Veo 2| Gemini 2.5 Pro| Gemini 2.0| Gemma 3| Claude Sonnet 3.7| Claude 3.5 Sonnet| Phi 4| Phi 3.5| Mistral Small 3.1| Mistral NeMo| Mistral-7b| Bedrock| Vertex AI| Qwen QwQ 32B| Qwen 2| Qwen 2.5 VL| Qwen Chat| Grok 3

AI Development Frameworks

n8n| LangChain| Agent SDK| A2A by Google| SmolAgents| LangGraph| CrewAI| Agno| LangFlow| AutoGen| LlamaIndex| Swarm| AutoGPT

Data Science Tools and Techniques

Python| R| SQL| Jupyter Notebooks| TensorFlow| Scikit-learn| PyTorch| Tableau| Apache Spark| Matplotlib| Seaborn| Pandas| Hadoop| Docker| Git| Keras| Apache Kafka| AWS| NLP| Random Forest| Computer Vision| Data Visualization| Data Exploration| Big Data| Common Machine Learning Algorithms| Machine Learning| Google Data Science Agent
πŸ‘ Av Logo White

Continue your learning for FREE

Forgot your password?
πŸ‘ Av Logo White

Enter OTP sent to

Edit

Wrong OTP.

Enter the OTP

Resend OTP

Resend OTP in 45s

πŸ‘ Popup Banner
πŸ‘ AI Popup Banner