Python

Dictionaries

Dictionaries in Python are an essential data type that allow you to store collections of data in an organised and efficient way. They are similar to lists in that they can hold multiple items, but unlike lists, dictionaries are unordered and are accessed using keys rather than indices.

A dictionary is created using curly braces {} and consists of a series of key-value pairs. The keys are used to access the values, and the values can be any type of data, including numbers, strings, lists, and even other dictionaries.

Here is an example of a dictionary in Python:

person = {
  "name": "John Smith",
  "age": 30,
  "skills": ["Python", "Java", "C++"]
}

In this example, the dictionary contains three key-value pairs:

  • “name” and “John Smith”
  • “age” and 30
  • “skills” and a list of programming languages.

To access the value of a key, you can use the syntax dictionary[key]. For example, to access John’s name, you would use person["name"].

person = {
  "name": "John Smith",
  "age": 30,
  "skills": ["Python", "Java", "C++"]
}

print(person['name'])
# Output "John Smith"

Dictionaries are useful for storing data that needs to be organized and accessed quickly, such as in a database or a lookup table. They are also commonly used in Python to represent complex data structures, such as JSON data.

You can add, modify, and delete elements in a dictionary using the same syntax as lists. For example, to add a new key-value pair to the dictionary, you can use person["email"] = “john@example.com”.

person = {
  "name": "John Smith",
  "age": 30,
  "skills": ["Python", "Java", "C++"]
}

person['email'] = "john@dictionaries.com"

print(person['email'])
# Outputs john@dictionaries.com

To delete a key-value pair, you can use the del keyword, such as del person["age"].

person = {
  "name": "John Smith",
  "age": 30,
  "skills": ["Python", "Java", "C++"],
  "email': "john@dictionaries.com"
}
del person['age']
print(person)
# Outputs {"name": "John Smith", "skills": ["Python", "Java", "C++"], "email": "john@dictionaries.com"}

We can also loop through a dictionary be accessing either just the keys or just the values. To access a tuple of both the key and it’s corresponding values, we can use a for loop with the .items() method.

person = {
  "name": "John Smith",
  "skills": ["Python", "Java", "C++"],
  "email": 'john@dictionaries.com'
}
for p in person.items():
    print(p)

# Output
# ('name', 'John Smith')
# ('skills', ['Python', 'Java', 'C++'])
# ('email', 'john@dictionaries.com')

To access just the keys, we can use the .keys() method.

person = {
  "name": "John Smith",
  "skills": ["Python", "Java", "C++"],
  "email": 'john@dictionaries.com'
}
for p in person.keys():
    print(p)

# Output
# name
# skills
# email

To access just the values, we can use the .values() method.

person = {
  "name": "John Smith",
  "skills": ["Python", "Java", "C++"],
  "email": 'john@dictionaries.com'
}
for p in person.values():
    print(p)

# Output
# "John Smith"
# ['Python', 'Java', 'C++']
# john@dictionaries.com