Python Dictionaries With Examples
A Python dictionary is discussed in this lesson with examples, the goal of which is to meet the needs of learners.
Dictionary Example:
“brand”: “Tesla”,
“model”: “Model-X”,
“year”: 2010
}
Dictionary In Python
Python dictionaries store key-value pairs of data values using key-value pairs.
Unlike a database, a dictionary is a collection of items that are ordered, changeable, and cannot be duplicated.
Dictionary entries are unordered in Python versions prior to 3.6. Dictionaries are now ordered in Python 3.7.
There are keys and values in Python dictionaries, and they are written in curly brackets {}:
A dictionary can be created and printed by following these steps:
Example
Dictionary Items
When it comes to Python dictionaries, item orders are maintained, changes are possible, and duplicates are not allowed.
Each dictionary item is given as a pair of key and value, and is referred to by its key name.
This is the dictionary’s “brand” value:
Example
Duplicates Not Allowed
The same key cannot be used for two items in a dictionary:
Existing values will be overwritten by duplicate values:
Example
Ordered or Unordered?
Since Python version 3.7, dictionaries are ordered – earlier versions of Python did not sort dictionaries.
Having an ordered dictionary means that the items have a defined order, and the order will not change for Python dictionaries.
An item that is unordered does not have an index, meaning that there is no defined order for it.
Changeable
Changing a dictionary means adding, removing or changing items after it has been created.
Dictionary Items – Data Types
Any data type can be used in dictionary items:
Integers, strings, booleans, and lists:
Example
A key is an integer, and a value is a string.
Below is an example of creating a dictionary called numbers.
Execute
Example
type()
Dicts are objects with the data type ‘dict’ from Python’s perspective:
A dictionary’s data type is printed as follows:
Example
Dictionary Length
Use the len() function to find the number of items in a dictionary:
This is how many items there are in the dictionary:
Example
Note that len() will also ignore duplicate items in the following examples:
Example: 
Python Collections (Arrays)
The Python programming language supports four types of collection data:
- Lists are ordered collections that can be rearranged. Duplicate members are allowed.
- Tuples are ordered and unchangeable collections. Duplicate members are allowed.
- Sets are unindexed and unordered collections. Duplicate members are not allowed.
- A dictionary is a collection of items that can be sorted and changed. Members are not duplicated in Python dictionaries.
Knowing a collection’s properties is helpful when choosing a type. When choosing the right type for a particular data set, meaning is retained, and efficiency and security are improved.
Dictionary Methods:
There are some Python dictonary methods listed below that we will discuss in depth with examples in our upcoming chapter on Python dictonary methods.
- clear()
- copy()
- get()
- items()
- keys()
- pop()
- popitem()
- update()
- values()
Python Dictionary Usage
Here are some common use cases for dictionaries:
- Dictionaries are often used to create mappings between related pieces of information. For example, you can use a dictionary to store a person’s name as the key and their corresponding phone number as the value.
- Dictionaries provide fast lookup based on keys. Instead of searching through the entire collection of data, you can directly access a specific value by using its associated key. This makes dictionaries efficient for tasks that require quick data retrieval.
- Dictionaries are useful for counting occurrences of elements in a dataset. For example, you can use a dictionary to count the frequency of words in a text or to track the occurrence of different items in a list.
- Dictionaries can be used to store and manage configuration settings. You can store various settings as key-value pairs, allowing for easy retrieval and modification of configuration values.
- Dictionaries can be utilized for caching and memorization purposes. By storing previously computed results as key-value pairs, you can avoid redundant calculations and improve the performance of your code.