About this question
A dictionary in Python is a collection of key-value pairs. You can easily create one using curly braces {} or the dict() function, and it allows you to store and retrieve values efficiently using unique keys.
A dictionary in Python is a collection of key-value pairs. You can easily create one using curly braces {} or the dict() function, and it allows you to store and retrieve values efficiently using unique keys.
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Python Expert
Answered on Apr 18, 2025
To create a new dictionary in Python, you can use two common approaches:
1. Using Curly Braces {}:
The simplest way to create a dictionary is by using curly braces {} and separating key-value pairs with a colon :. Multiple pairs are separated by commas.
Example:
my_dict = {"name": "John", "age": 25, "city": "New York"}
print(my_dict)In this example, "name", "age", and "city" are the keys, and their corresponding values are "John", 25, and "New York".
2. Using the dict() Constructor:
The dict() function is another way to create a dictionary. It is particularly useful when you want to create dictionaries from sequences of key-value pairs or when the keys are strings.
Example:
my_dict = dict(name="John", age=25, city="New York")
print(my_dict)Here, we use the dict() constructor to create the dictionary with the same key-value pairs.
Points to Note:
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Common Python interview questions, answered
Guides, tips and career advice on Python from JanBask experts.
Python How to Become a Python Developer: A Step-by-Step Guide for Beginners
Discover how to become a Python Developer via a practical 7-step guide build projects, maintain clean code, practice daily, and…
Python PCEP Certification Guide: Entry-Level Python Programmer Certification
Explore this PCEP certification guide for insights on cost, exam format, passing score, application, training, and study tips.…
Python Top 50+ Python Projects ideas for Beginners to Advanced
Discover innovative Python project ideas, explore advanced Python projects, and find good Python projects for beginners and…
Python How To Comment Out Multiple Lines In Python Like A Pro
Commenting out code is a common practice among programmers. It allows you to temporarily disable parts of your code without…