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:

vardict = {
“brand”: “Tesla”,
“model”: “Model-X”,
“year”: 2010
}

 

Python Dictionaries



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

vardict = { "brand": "Tesla", "model": "Tesla-Truck", "year": 2022 } print(vardict)

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

mrxdict = { "brand": "Tesla", "model": "Tesla Truck", "year": 2022 } print(mrxdict["brand"])

Duplicates Not Allowed

The same key cannot be used for two items in a dictionary:

Existing values will be overwritten by duplicate values:

Example

mrxdict = { "brand": "Tesla", "model": "ModelX", "year": 2010, "year": 2022 } print(mrxdict)

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

mrxdict = { "brand": "Tesla", "electric": True, "year": 2010, "colors": ["Maroon", "perl-white", "blue"] } print(mrxdict)

A key is an integer, and a value is a string.

Below is an example of creating a dictionary called numbers.

Execute

Example

numbers = {1: "One", 2: "Two", 3: "Three"} print(numbers)

type()

Dicts are objects with the data type ‘dict’ from Python’s perspective:

<class ‘dict’>

A dictionary’s data type is printed as follows:

Example

mrxdict = { "brand": "Tesla", "model": "Tesla-Truck", "year": 2010 } print(type(mrxdict))

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

mrxdict = { "brand": "Tesla", "model": "ModelX", "year": 2022 } print(len(mrxdict))

Note that len() will also ignore duplicate items in the following examples:

Example: 

mrxdict = { "brand": "Tesla", "model": "ModelX", "year": 2010, "year": 2022 } print(len(mrxdict))

Python Collections (Arrays)

The Python programming language supports four types of collection data:

  1. Lists are ordered collections that can be rearranged. Duplicate members are allowed.
  2. Tuples are ordered and unchangeable collections. Duplicate members are allowed.
  3. Sets are unindexed and unordered collections. Duplicate members are not allowed.
  4. 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.

  1. clear()
  2. copy()
  3. get()
  4. items()
  5. keys()
  6. pop()
  7. popitem()
  8. update()
  9. values()

Python Dictionary Usage

Here are some common use cases for dictionaries:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
Get the latest technical information straight to your email by subscribing to our Newsletter.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *