YouTip LogoYouTip

Python3 Dictionary

# Python3.x Python3 Dictionary A dictionary is another mutable container model that can store objects of any type. Each key-value pair in a dictionary is separated by a colon `:`, each pair is separated by a comma `,`, and the entire dictionary is enclosed in curly braces `{}`. The format is as follows: d = {key1 : value1, key2 : value2, key3 : value3 } **Note:** `dict` is a keyword and built-in function in Python, so it is not recommended to name your variable **dict**. !(#) Keys must be unique, but values do not need to be. Values can be of any data type, but keys must be immutable, such as strings or numbers. A simple dictionary example: tinydict = {'name': 'tutorial', 'likes': 123, 'url': 'www.'} !(#) You can also create a dictionary like this: tinydict1 = { 'abc': 456 } tinydict2 = { 'abc': 123, 98.6: 37 } * * * ## Creating an Empty Dictionary Use curly braces `{ }` to create an empty dictionary: ## Example # Use curly braces {} to create an empty dictionary emptyDict ={} # Print the dictionary print(emptyDict) # Check the length of the dictionary print("Length:",len(emptyDict)) # Check the type print(type(emptyDict)) The output of the above example is: {}Length: 0 Use the built-in function `dict()` to create a dictionary: ## Example emptyDict =dict() # Print the dictionary print(emptyDict) # Check the length of the dictionary print("Length:",len(emptyDict)) # Check the type print(type(emptyDict)) The output of the above example is: {}Length: 0 * * * ## Accessing Values in a Dictionary Place the corresponding key inside square brackets, as shown in the following example: ## Example #!/usr/bin/python3 tinydict = {'Name': 'Tutorial', 'Age': 7, 'Class': 'First'} print("tinydict['Name']: ", tinydict['Name'])print("tinydict['Age']: ", tinydict['Age']) The output of the above example is: tinydict['Name']: Tutorial tinydict['Age']: 7 If you try to access data using a key that does not exist in the dictionary, you will get an error like this: ## Example #!/usr/bin/python3 tinydict = {'Name': 'Tutorial', 'Age': 7, 'Class': 'First'} print("tinydict['Alice']: ", tinydict['Alice']) The output of the above example is: Traceback (most recent call last): File "test.py", line 5, in print ("tinydict['Alice']: ", tinydict['Alice'])KeyError: 'Alice' * * * ## Modifying a Dictionary You can add new content to a dictionary by adding new key/value pairs, or modify or delete existing key/value pairs, as shown in the following example: ## Example #!/usr/bin/python3 tinydict = {'Name': 'Tutorial', 'Age': 7, 'Class': 'First'} tinydict['Age'] = 8# Update Age tinydict['School'] = ""# Add information print("tinydict['Age']: ", tinydict['Age'])print("tinydict['School']: ", tinydict['School']) The output of the above example is: tinydict['Age']: 8 tinydict['School']: * * * ## Deleting Dictionary Elements You can delete a single element or clear the entire dictionary. Clearing requires only one operation. To explicitly delete a dictionary, use the `del` command, as shown in the following example: ## Example #!/usr/bin/python3 tinydict = {'Name': 'Tutorial', 'Age': 7, 'Class': 'First'} del tinydict['Name']# Delete key 'Name'tinydict.clear()# Clear the dictionary del tinydict# Delete the dictionary print("tinydict['Age']: ", tinydict['Age'
← Python3 DictionaryPython3 Iterator Generator β†’