DEV Community

Cover image for Dictionaries The Python Guide
Christopher Ambala
Christopher Ambala

Posted on • Edited on

Dictionaries The Python Guide

A dictionary in Python is an unordered collection of key-value pairs. Each item in the dictionary has a key, and each key maps to a unique value.

Dictionary

student = {
    "name": "John Doe",
    "age": 20,
    "grade": "Sophomore"
}
Enter fullscreen mode Exit fullscreen mode

"name", "age", and "grade" are keys, and "John Doe", 20, and "Sophomore" are their respective values.

Key Characteristics

  1. Unordered: Dictionaries are unordered, meaning the items do not have a defined order. The order of items can change over time, making the index of each item unreliable.
  2. Mutable: Dictionaries are mutable. This means you can change, add, and remove items after the dictionary is created.
  3. Indexed by Keys: Unlike lists, which are indexed by a range of numbers, dictionaries are indexed by keys. This key-value pair system makes dictionaries incredibly versatile for storing and organizing data.
  4. Cannot Contain Duplicate Keys: Each key in a dictionary must be unique. If a duplicate key is assigned a value, the original key’s value will be overwritten.

Dictionary Creation

Dictionaries are created by enclosing key-value pairs in curly braces {}.
A colon : separates keys from its associate value.

person = {"name": "Alice", "age": 25}

Enter fullscreen mode Exit fullscreen mode

Accessing Elements

print(person["name"])  # Output: Alice

Enter fullscreen mode Exit fullscreen mode

Modifying Elements

Dictionaries are mutable. You can change the value of a specific item by referring to its key.

Dictionaries are mutable. You can change the value of a specific item by referring to its key.
Enter fullscreen mode Exit fullscreen mode

Adding Elements

Adding an item to the dictionary is done by using a new index key and assigning a value to it.

person["city"] = "New York"
print(person)  # Output: {'name': 'Alice', 'age': 30, 'city': 'New York'}

Enter fullscreen mode Exit fullscreen mode

Removing Elements

Use the del statement.

del person["age"]
print(person)  # Output: {'name': 'Alice', 'city': 'New York'}

Enter fullscreen mode Exit fullscreen mode

Making a copy

Use the copy keyword

y = x.copy()
print(y)  # Output: {'one': 'uno', 'two': 2, 'three': 3}

Enter fullscreen mode Exit fullscreen mode

Removing all items

Use the clear keyword

x.clear()
print(x)  # Output: {}

Enter fullscreen mode Exit fullscreen mode

Getting the Number of Items

Use the len() keyword

x.clear()
print(x)  # Output: {}

Enter fullscreen mode Exit fullscreen mode

Looping Over values

for item in y.values():
    print(item)

Enter fullscreen mode Exit fullscreen mode

Using if statement to get values

if "one" in y:
    print(y['one'])  # Output: uno
if "two" not in y:
    print("Two not found")
if "three" in y:
    del y['three']

Enter fullscreen mode Exit fullscreen mode

Python dictionaries are a flexible and efficient data type that allow you to organize and manipulate data effectively. They are a fundamental part of Python and understanding them is crucial to becoming proficient in the language. Whether you’re storing configuration settings, managing data in a web application, or even building complex data structures, Python dictionaries are an excellent tool to have in your programming toolkit.

Top comments (0)