How to Initialize Dictionary in Python Last Updated : 28 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In Python dictionary, each key is unique and we can store different data types as values. The simplest and most efficient method to initialize a dictionary is using curly braces {}. This is the easiest and most common way to create a dictionary. We can use curly braces '{} ' and separate the keys and values with : colon. Each key-value pair is separated by a comma. Python a = {"name": "John", "age": 25, "city": "New York"} # Print the dictionary print(a) Output{'name': 'John', 'age': 25, 'city': 'New York'} Other methods we can use to initialise a dictionary are:Table of ContentUsing the dict() ConstructorUsing a Dictionary ComprehensionUsing fromkeys() MethodUsing dict() ConstructorAnother way to create a dictionary is by using the dict() constructor. This method is useful when you want to create a dictionary from a list of tuples or from keyword arguments. Python a = dict(name="Alice", age=30, city="Los Angeles") # Print the dictionary print(a) Output{'name': 'Alice', 'age': 30, 'city': 'Los Angeles'} We can also use list of key-value tuples to initialize a dictionary. Python a = dict([("name", "Bob"), ("age", 35), ("city", "Chicago")]) # Print the dictionary print(a) Output{'name': 'Bob', 'age': 35, 'city': 'Chicago'} Using a Dictionary ComprehensionDictionary comprehension is a more advanced method to create a dictionary, but it's still quite simple and powerful. It lets us create a dictionary by iterating over a sequence, such as a list or range. Python a = {x: x**2 for x in range(5)} # Print the dictionary print(a) Output{0: 0, 1: 1, 2: 4, 3: 9, 4: 16} Using fromkeys() MethodIf we want to create a dictionary with predefined keys and the same value for all keys, we can use the fromkeys() method. Python a = dict.fromkeys(["name", "age", "city"], "Unknown") # Print the dictionary print(a) Output{'name': 'Unknown', 'age': 'Unknown', 'city': 'Unknown'} Comment More infoAdvertise with us Next Article How to Initialize Dictionary in Python P pragya22r4 Follow Improve Article Tags : Python Python Programs python-dict Python dictionary-programs Practice Tags : pythonpython-dict Similar Reads How to Print a Dictionary in Python Python Dictionaries are the form of data structures that allow us to store and retrieve the key-value pairs properly. While working with dictionaries, it is important to print the contents of the dictionary for analysis or debugging.Example: Using print FunctionPython# input dictionary input_dict = 3 min read How to Initialize a Dictionary in Python Using For Loop When you want to create a dictionary with the initial key-value pairs or when you should transform an existing iterable, such as the list into it. You use string for loop initialization. In this article, we will see the initialization procedure of a dictionary using a for loop. Initialize Python Dic 3 min read How to Print Dictionary Keys in Python We are given a dictionary and our task is to print its keys, this can be helpful when we want to access or display only the key part of each key-value pair. For example, if we have a dictionary like this: {'gfg': 1, 'is': 2, 'best': 3} then the output will be ['gfg', 'is', 'best']. Below, are the me 2 min read How to Update a Dictionary in Python This article explores updating dictionaries in Python, where keys of any type map to values, focusing on various methods to modify key-value pairs in this versatile data structure. Update a Dictionary in PythonBelow, are the approaches to Update a Dictionary in Python: Using with Direct assignmentUs 3 min read Python | Initialize dictionary with None values Sometimes, while working with dictionaries, we might have a utility in which we need to initialize a dictionary with None values so that they can be altered later. This kind of application can occur in cases of memoization in general or competitive programming. Let's discuss certain ways in which th 4 min read Python - Initialize dictionary keys with Matrix Sometimes, while working with Python Data, we can have a problem in which we need to construct an empty mesh of dictionaries for further population of data. This problem can have applications in many domains which include data manipulation. Let's discuss certain ways in which this task can be perfor 4 min read Python - Custom dictionary initialization in list While working with Python, we can have a problem in which we need to initialize a list of a particular size with custom dictionaries. This task has itâs utility in web development to store records. Letâs discuss certain ways in which this task can be performed. Method #1 : Using {dict} + "*" operato 3 min read Python | Initialize list with empty dictionaries While working with Python, we can have a problem in which we need to initialize a list of a particular size with empty dictionaries. This task has it's utility in web development to store records. Let's discuss certain ways in which this task can be performed. Method #1 : Using {} + "*" operator Thi 5 min read Python | Initialize dictionary with multiple keys Sometimes, while working with dictionaries, we might have a problem in which we need to initialize the dictionary with more than one key with the same value. This application requirement can be in domains of web development in which we might want to declare and initialize simultaneously. Let's discu 8 min read Python | Initialize dictionary with common value While working with Python, sometimes, we might have a problem in which we require the initialize the static list into a dictionary with a constant value. Let's discuss certain ways in which this task can be performed. Method #1: Using dict() + list comprehension The combination of above functions ca 5 min read Like