Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Dictionary

A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values.

my_dict = {1 : 'Robin', 2 : 'Rahul', 3 : 'Aman'}
print(my_dict)
# Dictionary with the use of Mixed Keys.
my_dict = {'One' : 'Robin', 2 : 'Rahul'}
print(my_dict)

Accessing Items

You can access the items of a dictionary by referring to its key name, inside square brackets.

There is also a method called get() that will give you the same result.

my_dict = {1 : 'Robin', 2 : 'Rahul', 3 : 'Aman'}
val = my_dict[2]
print(val) # print 'Rahul'

# using get()
val = my_dict.get(2)
print(val) # print 'Rahul'

Change Values

You can change the value of a specific item by referring to its key name.

my_dict = {1 : 'Robin', 2 : 'Rahul', 3 : 'Aman'}
my_dict[2] = 'Rohan'
val = my_dict[2]
print(val) # print 'Rohan'

Loop through a Dictionary

You can loop through a dictionary by using a for loop.
When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.

my_dict = {1 : 'Robin', 2 : 'Rahul', 3 : 'Aman'}
for x in my_dict:
    print(x)    # print 1 2 3

# values() method to return values of a dictionary
for x in my_dict.values():
    print(x)    # print 'Robin' 'Rahul' 'Aman'

# Loop through both keys and values, by using the items() method
for key, val in my_dict.items():
    print(key, val)

Adding Items

Adding an item to the dictionary is done by using a new index key and assigning a value to it.

my_dict = {1 : 'Robin', 2 : 'Rahul', 3 : 'Aman'}
my_dict[4] = 'Shivam'  # Added item key is 4 and value is 'Shivam'
print(my_dict)

Removing Items

There are several methods to remove items from a dictionary.

The pop() method removes the item with the specified key name.

my_dict = {1 : 'Robin', 2 : 'Rahul', 3 : 'Aman'}
my_dict.pop(2) # Removes item with key 2 and value 'Rahul'
print(my_dict)

The del keyword removes the item with the specified key name.

my_dict = {1 : 'Robin', 2 : 'Rahul', 3 : 'Aman'}
del my_dict[2] # Removes item with key 2 and value 'Rahul'
print(my_dict)

Dict() constructor

It is also possible to use the dict() constructor to make a new dictionary.

my_dict = dict(1 = 'Robin', 2 = 'Rahul', 3 = 'Aman')
# note the use of equals rather than colon for the assignment
print(my_dict)

Try the following exercise in the editor below.

Given a dictionary called ‘my_dict’, perform the operations described in the comments:

  • # update value for key "Sunday" to 7
  • # adding another item with key "Default" having value 0
Start solving Dictionary on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test