Python Dictionaries



thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}


Dictionary

Dictionaries are used to store key data values: value pairs.

The dictionary is a compiled * collection, which is flexible and does not allow duplicates.


From version 3.7 of Python, dictionaries are in order. In Python 3.6 and earlier, dictionaries are not organized.


Dictionaries are written in italics, and have keys and numbers:


Example

Create and print a dictionary:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict)


Dictionary Items

Dictionary items are unique, flexible, and do not allow duplicates.

Dictionary items are presented with a key: in pairs of value, and can be referenced using a keyword.


Example

Print the "brand" value of the dictionary:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict["brand"])


Ordered or Unordered?


From version 3.7 of Python, dictionaries are in order. In Python 3.6 and earlier, dictionaries are not organized.


If we say that dictionaries are one thing, it means that things are in order, and that order is bound to change.

Disorder means the items do not have a specified order, you cannot refer to an item using the index.


Changeable

Dictionaries are flexible, meaning we can change, add or remove items once the dictionary has been created.


Duplicates Not Allowed

Dictionaries cannot have two objects with the same key:


Example

Duplicate values will overwrite existing values:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964,
  "year": 2020
}
print(thisdict)


Dictionary Length

To determine how many items a dictionary contains, use the len() function:


Example

Print the number of items in the dictionary:

print(len(thisdict))


Dictionary Items - Data Items

Prices for dictionary items can be for any type of data:


Example

String, int, boolean, and list data types:

thisdict = {
  "brand": "Ford",
  "electric": False,
  "year": 1964,
  "colors": ["red", "white", "blue"]
}


type()

From Python's point of view, dictionaries are defined as 'dictators':


<class 'dict'>


Example

Print the data type of a dictionary:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(type(thisdict))


Python Collections (Arrays)

There are four types of data collection in Python editing language:

  • The list is a ordered and flexible collection. Allows duplicate members.
  • Tuple is a ordered and unchanged collection. Allows duplicate members.
  • Set is a random, unstable *, and unidentified collection. No duplicate members.
  • The dictionary is a ordered order ** and is flexible. No duplicate members.

* Set items do not change, but you can delete and / or add items whenever you wish.

** Starting with version 3.7 of Python, dictionaries are in order. In Python 3.6 and earlier, dictionaries are not organized.


When choosing a type of collection, it is helpful to understand the characteristics of that type. Choosing the right type of specific data set may mean keeping the definition, and, it may mean increased efficiency or security.



Python - Access Dictionary Items



Accessing Items

You can access dictionary items by referring to its keyword, inside square brackets:


Example

Get the value of the "model" key:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
x = thisdict["model"]

There is also a method called get () that will give you the same result:


Example

Get the value of the "model" key:

x = thisdict.get("model")


Get Keys

The keys () method will return a list of all the keys to the dictionary.


Example

Get a list of the keys:

x = thisdict.keys()

A list of keywords is a dictionary view, which means that any changes made to the dictionary will be reflected in the key list.


Example

Add a new item to the original dictionary, and see that the keys list gets updated as well:

car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

x = car.keys()

print(x) #before the change

car["color"] = "white"

print(x) #after the change


Get Values

The values ​​() method will return a list of all values ​​to the dictionary.


Example

Get a list of the values:

x = thisdict.values()

The price list is a dictionary view, which means that any changes made to the dictionary will be reflected in the price list.


Example

Make a change in the original dictionary, and see that the values list gets updated as well:

car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

x = car.values()

print(x) #before the change

car["year"] = 2020

print(x) #after the change


Example

Add a new item to the original dictionary, and see that the values list gets updated as well:

car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

x = car.values()

print(x) #before the change

car["color"] = "red"

print(x) #after the change


Get Items

The items () method will return each item to the dictionary, such as tuples in the list.


Example

Get a list of the key:value pairs

x = thisdict.items()

The returned list is a view of dictionary items, which means that any changes made to the dictionary will be reflected in the list of items.


Example

Make a change in the original dictionary, and see that the items list gets updated as well:

car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

x = car.items()

print(x) #before the change

car["year"] = 2020

print(x) #after the change


Example

Add a new item to the original dictionary, and see that the items list gets updated as well:

car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

x = car.items()

print(x) #before the change

car["color"] = "red"

print(x) #after the change


Check if Key Exists

To determine if a specific key exists in the dictionary use the internal keyword:


Example

Check if "model" is present in the dictionary:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
if "model" in thisdict:
  print("Yes, 'model' is one of the keys in the thisdict dictionary")