Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is correct syntax to create Python dictionary?
A Dictionary is a collection of key-value pairs, where each key is separated from its value by a colon ":" and items are separated by commas. Dictionary keys must be unique and since Python 3.7, dictionaries maintain insertion order.
The correct syntax to create a Python dictionary is to store values in key-value pairs within curly braces { }. Keys appear on the left of the colon, values on the right, and items are separated by commas.
Basic Syntax
The fundamental syntax for creating a dictionary in Python ?
my_dict = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
Creating Dictionary with Multiple Key-Value Pairs
Dictionary with 4 Key-Value Pairs
Here's an example with keys Product, Model, Units, and Available ?
# Creating a Dictionary with 4 key-value pairs
product_info = {
"Product": "Mobile",
"Model": "XUT",
"Units": 120,
"Available": "Yes"
}
print("Dictionary =", product_info)
Dictionary = {'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}
Dictionary with 5 Key-Value Pairs
Adding one more key-value pair to the previous example ?
# Creating a Dictionary with 5 key-value pairs
product_info = {
"Product": "Mobile",
"Model": "XUT",
"Units": 120,
"Available": "Yes",
"Grade": "A"
}
print("Dictionary =", product_info)
Dictionary = {'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes', 'Grade': 'A'}
Different Key Types
Dictionary with String Keys
String keys are the most common type, where each key is a text label ?
person = {
"name": "Niharika",
"age": 30,
"city": "New York",
"company": "TutorialsPoint"
}
print("Type:", type(person))
print("Dictionary =", person)
Type: <class 'dict'>
Dictionary = {'name': 'Niharika', 'age': 30, 'city': 'New York', 'company': 'TutorialsPoint'}
Dictionary with Integer Keys
Integer keys can be useful for numerical mappings ?
subjects = {
101: "Math",
102: "Science",
103: "History"
}
print("Type:", type(subjects))
print("Dictionary =", subjects)
Type: <class 'dict'>
Dictionary = {101: 'Math', 102: 'Science', 103: 'History'}
Mixed Data Type Dictionary
Python dictionaries can contain different data types for both keys and values ?
mixed_dict = {
"name": "Niharika",
1: [10, 20, 30],
"is_active": True,
3.14: "pi"
}
print("Type:", type(mixed_dict))
print("Dictionary =", mixed_dict)
Type: <class 'dict'>
Dictionary = {'name': 'Niharika', 1: [10, 20, 30], 'is_active': True, 3.14: 'pi'}
Creating an Empty Dictionary
An empty dictionary can be created and populated later ?
empty_dict = {}
print("Type:", type(empty_dict))
print("Dictionary =", empty_dict)
# Adding items later
empty_dict["key1"] = "value1"
print("After adding item:", empty_dict)
Type: <class 'dict'>
Dictionary = {}
After adding item: {'key1': 'value1'}
Using the dict() Function
The dict() function provides alternative ways to create dictionaries from keyword arguments or key-value pairs ?
# Using keyword arguments
employee = dict(name="Riyaansh", age=28, department="Python")
print("From keywords:", employee)
# Using list of tuples
data = dict([(1, "one"), (2, "two"), (3, "three")])
print("From tuples:", data)
# Using dict() with empty arguments
empty_dict = dict()
print("Empty dict:", empty_dict)
From keywords: {'name': 'Riyaansh', 'age': 28, 'department': 'Python'}
From tuples: {1: 'one', 2: 'two', 3: 'three'}
Empty dict: {}
Conclusion
Python dictionaries are created using curly braces {} with key-value pairs separated by colons and commas. You can also use the dict() function for more flexible creation methods. Remember that keys must be unique and immutable types.
