Introduction to “Dictionaries” in Python
10 min readDec 18, 2022
What is a “Dictionary”?
- A “Python Dictionary” “Maps” the “Keys” to the “Values”, and, in “Other Programming Languages”, knows as a “Map”, or, an “Associative Array”.
- Each of the “Key-Value Pair” in a “Python Dictionary” is “Called” an “Item”.
How “Python Dictionaries” are “Created”?
- “Python Dictionaries” are “Created” using the “Curly Braces”, i.e., “{}”, “Containing” the “Key-Value Pairs”.
- “Each” of the “Pairs” within a “Dictionary” is “Separated” by a “Comma”, i.e., “,”.
- “Each” of the “Keys” within “Each” of the “Pairs” in a “Dictionary” is “Separated” from the corresponding “Values” by a “Colon”, i.e., “:”.
- The “Values” in “Each” of the “Key-Value Pairs” are “Accessible” via the corresponding “Keys”.
- Since, “Each Key” is “Associated” with “Exactly One Value”, and, “Lookup” in a “Python Dictionary” is “Performed” through the “Keys”, the “Keys” must be “Unique” within any “Python Dictionary”.
- It is fine to have “Duplicate Values” in a “Python Dictionary”.
Create an “Empty Dictionary”
- It is possible to “Create” an “Empty Dictionary” by using an “Empty Curly Braces”.
Create a “Dictionary” From “Other Collection Data Types”
- “Python Dictionaries” can be “Created” from “Other Collection Data Types”, such as — “Tuples”, using the “dict () Constructor”.
- The “dict () Constructor” can be used to “Convert” from an “Iterable Series” of “Key-Value Pairs” that are “Stored” in “Other Collection Data Types”, such as - “Tuples”.
Create a “Dictionary” From “Keyword Arguments”
- So long as the “Keys” in a “Python Dictionary” are “Legitimate Python Identifiers”, it is even possible to “Create” a “Python Dictionary” directly from “Keyword Arguments” that are “Passed” to the “dict () Constructor”.
Python Dictionary Internals
- Internally, the “Python Dictionary” “Maintains” the “Pairs” of “References” to the “Key” Objects, and, the “Value” Objects respectively.
- The “Key” Objects must be “Immutable”. So, “Strings”, “Numbers”, and, “Tuples” can be used as “Key”, but, “Lists” can not be used.
- The “Value” Objects can be “Mutable”, and, in practice, often are.
“Order” of “Items” in “Python Dictionary”
- In “Python Versions Prior to 3.7”, the “Order” of the “Items” in a “Python Dictionary” “Couldn’t” be relied upon. It is essentially “Random”, and, many even “Vary” between the “Different Runs” of the “Same Program”.
- As of “Python Versions 3.7”, however, the “Order” of the “Items” in a “Python Dictionary” are required to be “Kept” in the “Insertion Order”.
“Retrieve” the “Values” of “Items” By Corresponding “Keys” From a “Dictionary”
- It is possible to “Retrieve” the “Value” of an “Item” by the corresponding “Key” from a “Dictionary” using the “Square Brackets Operator”, i.e., “[]”.
- If the “Data Type” of the “Key” is “String”, then the “Key” should be “Put” inside the “Square Brackets Operator” “Inside Quotation”.
- Example - To “Retrieve” the “Value” of an “Item” by the corresponding “Key”, e.g., “Soumyajyoti”, from a “Dictionary”, the “Key”, i.e., “Soumyajyoti” should be “Put” inside the “Square Brackets Operator” as following - “[“Soumyajyoti”]”.
- If the “Key” of an “Item” to be “Retrieved” from a “Dictionary” is “Not Present” in the “Dictionary”, the “KeyError Exception” is “Thrown”.
“Retrieve” the “Values” of “Items” By Corresponding “Keys” From a “Dictionary” Using “get ()” Method
- It is possible to “Retrieve” the “Value” of an “Item” by the corresponding “Key” from a “Dictionary” using the “get ()” Method.
- If the “Data Type” of the “Key” is “String”, then the “Key” should be “Put” inside the “get ()” Method “Inside Quotation”.
- If the “Data Type” of the “Key” is “Integer”, then the “Key” should be “Put” inside the “get ()” Method “Without Quotation”.
- If the “Key” of an “Item” to be “Retrieved” from a “Dictionary”, using the “get ()” Method, is “Not Present” in the “Dictionary”, “No Exception” is “Thrown”. Instead “None” is “Returned”.
- When “Retrieving” the “Value” of an “Item” by “Key” from a “Dictionary”, using the “get ()” Method, if the “Key” of the “Item” to be “Retrieved” is “Not Present” in the “Dictionary”, it is possible to assign a “Default Value” to be “Returned” instead “None”.
- Provide the “Default Value” to be “Returned” as the “Second Argument” to the “get ()” Method.
- When the “Default Value” is provided as the “Second Argument”, if the “Key” of an “Item” to be “Retrieved” is “Not Present” in the “Dictionary”, then the “Default Value” will be “Returned”.
- When the “Default Value” is provided as the “Second Argument”, if the “Key” of an “Item” to be “Retrieved” is “Present” in the “Dictionary”, then the corresponding “Value” will be “Returned”.
How “Existing Values” of “Items” Can Be “Replaced” With “New Values” By Corresponding “Keys” From a “Dictionary”?
- It is possible to “Replace” the “Existing Value” of an “Item” with a “New Value” by “Putting” the associated “Key” inside the “Square Brackets Operator” and “Assigning” the “New Value” to it.
“Add New Items” Into a “Python Dictionary”
- If a “New Value” is “Assigned” to a “Key”, which “Does Not Exist” in the “Python Dictionary”, by “Putting” the “Key” inside the “Square Brackets Operator” and “Assigning” the “New Value” to it, then a “New Item” would be “Added” into the “Python Dictionary”.
“Add” the “Items” From “One Dictionary” to “Another Dictionary”
- It is possible to “Extend” an “Existing Python Dictionary” with the “Definitions” from “Another Existing Python Dictionary” using the “update ()” Method.
- The “update ()” Method is “Called On” the “Dictionary” “To Be Updated”, and, “Takes” the “Contents” of the “Dictionary”, which needs to be “Merged In”, as the “Argument”.
- In the “Argument” of the “update ()” Method, if the “Contents” of the “Dictionary” that needs to be “Merged In” with the “Target Dictionary” includes the “Keys”, which are “Already Present” in the “Target Dictionary”, then the “Values” associated with these “Keys” are “Replaced” in the “Target Dictionary” by the corresponding “Values” from the “Source Dictionary” that is “Sent” as the “Argument” to the “update ()” Method.
“Iterating Over” the “Items” of “Python Dictionary”
- Since “Python Dictionaries” are “Iterables”, the “for Loop” can be used to “Iterate Over” the “Items” of “Python Dictionaries”.
“Iterating Over” the “Items” of “Python Dictionary” Using “Keys” on “Each Iteration”
- The “Python Dictionary” “Yields” the “Key” on “Each Iteration”, and, the corresponding “Value” can be “Retrieved” through “Look up” using the “Square Brackets Operator”.
- It is possible to “Iterate Over” the “Keys” of a “Python Dictionary” using the “for Loop”.
“Iterating Over” the “Items” of “Python Dictionary” to “Retrieve” Only “Keys” Using “keys ()” Method
- It is possible to “Iterate Over” only the “Keys” of a “Python Dictionary” by “Calling” the “keys ()” Method on the “Python Dictionary” in the “for Loop”.
- The “keys ()” Method “Returns” an “Object”, which provides an “Iterable View” onto the “Keys” of “Python Dictionary”, “Without” causing the “Keys” to be “Copied”.
“Iterating Over” the “Items” of “Python Dictionary” to “Retrieve” Only “Values” Using “values ()” Method
- It is possible to “Iterate Over” only the “Values” of a “Python Dictionary” by “Calling” the “values ()” Method on the “Python Dictionary” in the “for Loop”.
- The “values ()” Method “Returns” an “Object”, which provides an “Iterable View” onto the “Values” of “Python Dictionary”, “Without” causing the “Values” to be “Copied”.
- There is “No Efficient / Convenient Way” to “Retrieve” the corresponding “Key” from a “Value”.
“Iterating Over” the “Keys” and “Values” of “Python Dictionary” in “Tandem” Using “items ()” Method
- It is possible to “Iterate Over” the “Keys”, and, the “Values” of a “Python Dictionary” in “Tandem”.
- It is possible to get hold of an “Iterable View” of the “Items” of a “Python Dictionary” using the “items ()” Method.
- When “Iterated”, the “Items View” “Yields” “Each” of the “Key-Value Pairs” of a “Python Dictionary” as a “Tuple”.
- By using the “Tuple Unpacking” in the “for Statement”, it is possible to “Retrieve” both the “Keys”, and, the “Values” of a “Python Dictionary” in “One Operation” “Without” the “Extra Look up”.
How “Existing Items” From a “Python Dictionary” Can Be “Copied” Into “Another Python Dictionary”?
- “Copying Dictionaries” is “Shallow” by default, i.e., “Copying” only the “References” to the “Key” and “Value” Objects in the “Items”, and, “Not” the “Items”.
- There are “Two Ways” of “Copying Dictionaries”, of which, the “Second Way” is the “Most Common” one.
“Copy” an “Already Existing Python Dictionary” into a “New Python Dictionary” Using “copy ()” Method
- The “copy ()” Method is “Called On” the “Existing Python Dictionary” to “Copy” “All the Items” of that “Existing Python Dictionary” to a “New Python Dictionary”.
“Copy” an “Already Existing Python Dictionary” into a “New Python Dictionary” Using “dict () Constructor”
- “All the Items” of an “Existing Python Dictionary” can be “Copied” into a “New Python Dictionary” by “Passing” the “Existing Python Dictionary” to the “dict () Constructor”.
- Although, it is a “Mattre of Taste”, still it is “Preferrable” to use the “dict () Constructor” approach to “Copy” “All the Items” of an “Already Existing Python Dictionary” into a “New Python Dictionary”, since, this approach has the “Advantage” of “Working” with “Any Iterable Series” as the “Source”, and, “Not Just Dictionaries”.
“Membership Verification” of the “Items” in a “Dictionary”
- The “Membership Tests” for “Python Dictionaries” work on the “Keys”.
- “Membership” is a “Fundamental Operation” for “Dictionaries”.
- As with “Other Collection Data Types”, the “Membership Operation” is “Performed” using the “IN” and “NOT IN” Operators on “Dictionaries”.
“Membership Verification” of the “Keys” of an “Item” in a “Dictionary” Using “IN Operator”
- To “Verify” that “If” a “Key” of an “Item” is “Present” in a “Dictionary” using “Membership”, then “IN Operator” is used.
- If the “Key” of an “Item” is “Present” in the “Dictionary”, “True” is “Returned”.
- If the “Key” of an “Item” is “Not Present” in the “Dictionary”, “False” is “Returned”.
“Membership Verification” of the “Keys” of an “Item” in a “Dictionary” Using “NOT IN Operator”
- To “Verify” that “If” a “Key” of an “Item” is “Not Present” in a “Dictionary” using “Membership”, then “NOT IN Operator” is used.
- If the “Key” of an “Item” is “Not Present” in the “Dictionary”, “True” is “Returned”.
- If the “Key” of an “Item” is “Present” in the “Dictionary”, “False” is “Returned”.
“Remove” the “Items” from a “Dictionary”
- There are “Multiple Methods available to “Remove” the “Items” from a “Dictionary”.
“Remove” the “Items” from a “Dictionary” Using the “del” Keyword
- It is possible to “Remove” an “Items” from a “Dictionary” using the “del Keyword”.
- The “del Keyword” “Takes” a “Single Parameter”, which is the “Key” of the “Item” to be “Deleted” from the “Dictionary”, and, “Removes” the “Item” from the “Dictionary”.
“Remove” the “Items” from a “Dictionary” Using the “pop ()” Method
- It is possible to “Remove” an “Items” from a “Dictionary” by its “Key” using the “pop ()” Method.
- The “pop ()” Method “Takes” a “Single Parameter”, which is the “Key” of the “Item” to be “Deleted”, and, “Returns” the “Value” of the “Deleted Item”.
“Remove” the “Last Item” from a “Dictionary” Using the “popitem ()” Method
- It is possible to “Remove” the “Last Item” from a “Dictionary” using the “popitem ()” Method.
- The “popitem ()” Method “Does Not Take” a “Parameter”, and, “Returns” the “Deleted Item” as a “Tuple”.
- Hence, by using the “Tuple Unpacking”, it is possible to “Retrieve” both the “Key”, and, the “Value” of the “Deleted Item” from the “Python Dictionary”.
“Remove” “All” the “Items” from a “Dictionary” Using the “clear ()” Method
- It is possible to “Remove” “All” the “Items” from a “Dictionary” using the “clear ()” Method.