Skip to content

Dictionaries

Dictionaries are unordered collections of items. Data is stored in key value pairs and keys should be unique and immutable.

1
2
3
empty_dict = {}
print(empty_dict)
print(type(empty_dict))

{}

empty_dict = dict()
print(empty_dict)

{}

student = {"name": "John", "age": 20, "grade": "A"}
print(student)

{'name': 'John', 'age': 20, 'grade': 'A'}

1
2
3
print("Accessing dictionary elements")
student = {"name": "John", "age": 20, "grade": "A"}
print(student["grade"])

Accessing dictionary elements

A

1
2
3
4
5
print("Accessing dictionary elements")
student = {"name": "John", "age": 20, "grade": "A"}
print(student.get("grade"))
print(student.get("last_name"))
print(student.get("last_name", "Not available"))

Accessing dictionary elements

A

None

Not available

1
2
3
4
5
6
7
8
9
print("Modifying dictionary elements")
student = {"name": "John", "age": 20, "grade": "A"}
print(student)
student["age"] = 21
print(student)
student["last_name"] = "Doe"
print(student)
del student["grade"]
print(student)

Modifying dictionary elements

{'name': 'John', 'age': 20, 'grade': 'A'}

{'name': 'John', 'age': 21, 'grade': 'A'}

{'name': 'John', 'age': 21, 'grade': 'A', 'last_name': 'Doe'}

{'name': 'John', 'age': 21, 'last_name': 'Doe'}

student = {"name": "John", "age": 20, "grade": "A"}
student.keys()

dict_keys(['name', 'age', 'grade'])

student = {"name": "John", "age": 20, "grade": "A"}
student.values()

dict_values(['John', 20, 'A'])

student = {"name": "John", "age": 20, "grade": "A"}
student.items()

dict_items([('name', 'John'), ('age', 20), ('grade', 'A')])

1
2
3
4
5
6
student = {"name": "John", "age": 20, "grade": "A"}
student_copy = student
print(student_copy)
student["age"] = 21
print(student_copy)
print(student)

{'name': 'John', 'age': 20, 'grade': 'A'}

{'name': 'John', 'age': 21, 'grade': 'A'}

{'name': 'John', 'age': 21, 'grade': 'A'}

1
2
3
4
5
6
student = {"name": "John", "age": 20, "grade": "A"}
student_shallow_copy = student.copy()
print(student_shallow_copy)
student["age"] = 22
print(student_shallow_copy)
print(student)

{'name': 'John', 'age': 20, 'grade': 'A'}

{'name': 'John', 'age': 20, 'grade': 'A'}

{'name': 'John', 'age': 22, 'grade': 'A'}

1
2
3
student = {"name": "John", "age": 20, "grade": "A"}
for keys in student.keys():
    print(keys)

name

age

grade

1
2
3
student = {"name": "John", "age": 20, "grade": "A"}
for keys in student.values():
    print(keys)

John

20

A

1
2
3
student = {"name": "John", "age": 20, "grade": "A"}
for key, value in student.items():
    print(key, value)

name John

age 20

grade A

1
2
3
print("Dictionary comprehension")
squares = {x: x**2 for x in range(1, 6)}
print(squares)

Dictionary comprehension

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

1
2
3
print("Dictionary comprehension")
squares = {x: x**2 for x in range(1, 6) if x % 2 == 0}
print(squares)

Dictionary comprehension

{2: 4, 4: 16}

1
2
3
4
print("Counting occurrences of numbers in a list")
numbers = [1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4]
number_count = {num: numbers.count(num) for num in set(numbers)}
number_count

Counting occurrences of numbers in a list

{1: 4, 2: 3, 3: 3, 4: 6}

1
2
3
4
5
print("Merging dictionaries")
dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4}
merged_dict = {**dict1, **dict2}
print(merged_dict)

Merging dictionaries

{'a': 1, 'b': 3, 'c': 4}