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 Like