89DEVs

Python: How to create an empty Dictionary?

How to create an empty Dictionary in Python?

To create an empty dictionary in Python, use curly brackets or the dict() function. Using curly brackets is faster and more efficient than using the dict() function. It is therefore recommend to prefer curly brackets over the dict() function for creating an empty Dictionary in Python. It is also considered better style and a more pythonic way of doing it.

create empty Dictionary using curly brackets

To create an empty dictionary in Python, use curly brackets like shown below. # create empty dictionary using curly brackets myDictionary = {} print(myDictionary) The dictionary is created and printed. {} To verify the type of the created object, use the type() function. # verify the type of the created object myDictionary = {} myDictionary == {} The variable myDictionary is compared to an empty list. True is returned to indicate that the variable contains an empty list. True

create an empty dictionary using dict()

To create an empty dictionary, use the dict() without any argument. # create an empty dictionary using dict() myEmptyDictionary = dict() myNonEmptyDictionary = dict(one=1, two=2, three=3) print(myEmptyDictionary) print(myNonEmptyDictionary) In this example two dictionaries are created. The first dictionary is an empty dictionary, created by using the dict() function without any arguments. The second dictionary is an non-empty dictionary, created by using the dict() function and passing values to it. Finally both dictionaries are printed. {} {'one': 1, 'two': 2, 'three': 3}

add elements to an empty dictionary

Unlike lists or tuples, the dictionary object doesn't offer add(), insert() or append() methods. Instead elements can be added by creating a new index key and passing a value. The syntax to add elements to an dictionary is: dictionary[key] = value In the following example four element are added to the empty dictionary. # adding elements to an empty array # create the empty dictionary myDict = {} # add element 1 myDict['one'] = 1 # add element 2 myDict['two'] = 2 # add element 3 myDict['three'] = 3 # add element 4 myDict['four'] = 4 # print the dictionary print(myDict) The empty dictionary is created, then four elements are added to it one by one. Finally the dictionary is printed. It contains all four elements, which have been added to it. {'one': 1, 'two': 2, 'three': 3, 'four': 4} To update an existing value in the dictionary use the same syntax and just pass a new value to an existing key. # updating key 'four' myDict['four'] = 0 print(myDict) The fourth element is updated to integer 0 and the updated dictionary is printed. {'one': 1, 'two': 2, 'three': 3, 'four': 0}

check if a dictionary is empty

To check if an list is empty use the if not operator. # check if a dictionary is empty myDict = {} if not myDict: True The dictionary myDict is empty, therefore True is returned. True Other options to check if a dictionary is empty are, using the len() function to find the length of the dictionary and comparing the list to an empty list. The if not operator is the preferred way of checking if a dictionary is empty.

                
        

Summary


Click to jump to section