• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Python » Dictionary in Python
Next →
← Prev

Dictionary in Python

By Dinesh Thakur

In this tutorial, I’m going to show you how to use dictionaries in Python. How to build, view, add, delete elements from them and different built-in techniques.

We’ll be covering the following topics in this tutorial:

  • What are Dictionaries in Python
  • Accessing Elements from Dictionary
  • Dictionary Items – Data Types
  • Dictionary Length
  • Updating, adding and deleting Dictionary elements
  • Python Dictionary clear()
  • Python Dictionary del()
  • Python Dictionary keys()
  • Python Dictionary values()
  • Python Dictionary items()

What are Dictionaries in Python

Dictionary in Python are like associative lists or a map. Now you can think of a dictionary as a list of key:value pairs. A dictionary is an unordered, changeable collection which does not allow duplicates. Let me show you how to define a dictionary.

D = {'Name': 'max', 'age': 14, 'year': 2020}

You can specify any variable name and define a dictionary, first of all, you use these curly brackets {}, and inside these curly brackets, you provide a list of key-value pairs. Let’s give a list of key-value pairs.

Accessing Elements from Dictionary

I’m going to access the items inside this dictionary. Which is D, and you can see our dictionary is created. Now, as I said dictionary is a list of key-value pairs and all these values. Which you see here before the colon are called keys. Name is a key here, and age is a key, the year is key. Whatever you see after the colon are called values, so max is a value. 2020 is a value and 14 is a value, and you can access the values from a dictionary-based upon their keys. for example

#!/usr/bin/python
D = {'Name': 'max', 'age': 14, 'year': 2020}
print(D['Name'])
Output: max

Above dictionary D. I can use the square bracket [] and the key name, for example, I want to get the name value. I can give the name key here. It’s going to return me the Associated value related to key name in the same way you can use other keys also. for example

#!/usr/bin/python
D = {'Name': 'max', 'age': 14, 'year': 2020}
print(D['age'])
Output: 14

It’s going to give me 14. which is the value. Age is a key here, and 14 is the value.

Dictionary Items – Data Types

You can define any data type as a key. Let me explain a new dictionary here, and I’m going to give the curly brackets {} and as I said you could define a string value as key. You can specify a number as a key. for example

E = {'Name': 'Tom', 15: 15, 15.1:15.1, True: True, (2,3): 5}

Now to access let’s say we want to access the value for (2,3) key. which is tuple. it’s going to return me five. it’s going to return me the value which is associated with it.

#!/usr/bin/python
E = {'Name': 'Tom', 15: 15, 15.1:15.1, True: True, (2,3): 5}
print(E[(2,3)])
Output: 5

Now, what happens when a key is not there, and we try to access it. I’m going to access a hundred from this a dictionary. It’s going to give us an error that this key is not present in the dictionary.

#!/usr/bin/python
E = {'Name': 'Tom', 15: 15, 15.1:15.1, True: True, (2,3): 5}
print(E[100])
Output: KeyError: 100

Dictionary Length

You can also use the len() method to find out the number of items in the dictionary, and you can see it says five items are there in the dictionary.

#!/usr/bin/python
E = {'Name': 'Tom', 15: 15, 15.1:15.1, True: True, (2,3): 5}
print(len(E))
Output: 5

Above len() function will return you the number of key-value pairs stored in the dictionary.

You can also use, for example, I’m going to use my D dictionary. Now and you can also use a method called get() and then give the key name here in the parenthesis. Let’s say I want to get that value associated with the name key. I can get it.

#!/usr/bin/python
D = {'Name': 'max', 'age': 14, 'year': 2020}
print(D.get('Name'))
Output: max

it’s going to give me the value associated with the name key.

Updating, adding and deleting Dictionary elements

You can also add a new key to D dictionary. Now three key-value pairs and I can add one more key-value pair to add a key-value pair you need to write D and in the square bracket []. It would be best if you gave the name of the new key. I’m going to write surname here. The name of the new key in the dictionary D and then you need to give the value associated with that key.

#!/usr/bin/python
D = {'Name': 'max', 'age': 14, 'year': 2020}
D['Surname'] = 'Thakur'
print(D)
Output: {'year': 2020, 'Surname': 'Thakur', 'age': 14, 'Name': 'max'}

You can see that surname is added to your dictionary.

If you want to remove any key-value pair from a list, you can use the pop() method and then the name of the key, which you want to remove. Let’s say we want to remove the surname, which we have added.

#!/usr/bin/python
D = {'year': 2020, 'Surname': 'Thakur', 'age': 14, 'Name': 'max'}
D.pop('Surname')
print(D)
Output: {'Name': 'max', 'age': 14, 'year': 2020} 

You can update the values in a dictionary. In D dictionary I want to update the name for example. I can use the dictionary name and then the key. for example

#!/usr/bin/python
D = {'year': 2020, 'Surname': 'Thakur', 'age': 14, 'Name': 'max'}
D['Name'] = 'Dinesh'
print(D)
Output: {'year': 2020, 'age': 14, 'Surname': 'Thakur', 'Name': 'Dinesh'} 

Python Dictionary clear()

You can also use a clear() function. Let’s see what’s there in the D dictionary. These are the values inside the dictionary. I can use clear the values inside the dictionary.

#!/usr/bin/python
D = {'year': 2020, 'Surname': 'Thakur', 'age': 14, 'Name': 'max'}
D.clear()
print(D)
Output: {}

When I try to access D, it will give me the empty dictionary {}.

Python Dictionary del()

You can delete the dictionary using the del() function and the name of the dictionary.

#!/usr/bin/python
D = {'year': 2020, 'Surname': 'Thakur', 'age': 14, 'Name': 'max'}
del D
print(D)
Output: NameError: name 'D' is not defined 

When I try to access this dictionary D. it’s going to say that D name is not defined.

Python Dictionary keys()

Now there is a function in a dictionary called keys(). Which is used to list out all the keys of that dictionary. you can see

#!/usr/bin/python
D = {'year': 2020, 'Surname': 'Thakur', 'age': 14, 'Name': 'max'}
print(D.keys())
Output: dict_keys(['age', 'Name', 'Surname', 'year']) 

It will list out all the keys of the particular dictionary.

Python Dictionary values()

There is also a function called values(), which will list all the dictionary values if you want to list out all the key-value pairs.

#!/usr/bin/python
D = {'year': 2020, 'Surname': 'Thakur', 'age': 14, 'Name': 'max'}
print(D.values())
Output: dict_values(['max', 2020, 'Thakur', 14]) 

Python Dictionary items()

You can use the function called items. It will give you the key-value pair list.

#!/usr/bin/python
D = {'year': 2020, 'Surname': 'Thakur', 'age': 14, 'Name': 'max'}
print(D.items())
Output: dict_items([('year', 2020), ('Surname', 'Thakur'), ('Name', 'max'), ('age', 14)])

You’ll also like:

  1. What is Metadata OR Data Dictionary?
  2. What is Python? | Introduction to Python Programming
  3. Python Features | Main Features of Python Programming Language
  4. Python Operators and Operands – Types of Operators in Python
  5. While loop in Python
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Python

Python Tutorials

  • Python - Home
  • Python - Features
  • Python - Installation
  • Python - Hello World
  • Python - Operators Types
  • Python - Data Types
  • Python - Variable Type
  • Python - Switch Case
  • Python - Line Structure
  • Python - String Variables
  • Python - Condition Statement
  • Python - if else Statement
  • Python - for-loop
  • Python - while loop
  • Python - Command Line
  • Python - Regular Expression

Python Collections Data Types

  • Python - List
  • Python - Sets
  • Python - Tuples
  • Python - Dictionary

Python Functions

  • Python - Functions
  • Python - String Functions
  • Python - Lambda Function
  • Python - map() Function

Python Object Oriented

  • Python - Oops Concepts
  • Python - File Handling
  • Python - Exception Handling
  • Python - Multithreading
  • Python - File I/O

Python Data Structure

  • Python - Linked List
  • Python - Bubble Sort
  • Python - Selection Sort
  • Python - Linear Search
  • Python - Binary Search

Python Programs

  • Python - Armstrong Number
  • Python - Leap Year Program
  • Python - Fibonacci Series
  • Python - Factorial Program

Other Links

  • Python - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW