Introduction to
Dictionaries in Python
This presentation covers Python dictionaries. Learn about
creating, accessing, and modifying dictionaries. Explore
nested dictionaries and common methods.
by ELIMU JONATHAN
MUKOMAZI HUSSEIN
SEMAKULA TREVOR
What are Dictionaries?
1 Definition 2 Key Characteristics
Dictionaries store data Keys are unique and
as key-value pairs. immutable. Values can
They are mutable and be of any type.
efficient for lookups.
3 Why Use Dictionaries?
Fast access to values using keys. Useful for structured data.
Creating and Accessing
Dictionaries
Creating a Dictionary
Use curly braces {} or the dict() constructor.
Accessing Values
Use the key inside square brackets [] or the .get() method.
Handling Missing Keys
.get() avoids KeyError by returning None or a default value if key
doesn't exist.
Modifying and Updating Dictionaries
Adding or Updating Items Deleting Items Iterating Over a Dictionary
Assign a value to a new or existing key. Use del, .pop(), or .clear(). Loop through keys, values, or key-value
pairs.
Nested Dictionaries
What Are Nested Accessing Nested
Dictionaries? Values
A dictionary that contains another Use multiple keys to access nested
dictionary as its value. values.
Modifying Nested Values
Update values within nested dictionaries.
Dictionary Methods
.keys() .values() .items()
Returns a view object of Returns a view object of Returns a view object of
all keys. all values. key-value pairs.
Explore common dictionary methods. Learn about view methods and other useful
functions.
Other Useful Methods
.update()
1 Adds key-value pairs from another dictionary.
.setdefault()
2 Returns the value of a key, or sets a default if the key doesn’t exist.
.copy()
3 Creates a shallow copy of the dictionary.
Dictionary Comprehension
Creating Dictionaries Syntax Example
Dictionary comprehension offers a concise {key: value for item in iterable if condition} Create a dictionary of squares: {x: x**2 for x
way to create dictionaries in Python. in range(6)}.
Conclusion
1 Key Takeaways 2 Nested Structures
Dictionaries are powerful for Nested dictionaries are useful
storing and accessing data for hierarchical data.
efficiently.
3 Methods
Use methods like .get(), .update(), and .copy() effectively.
Next Steps
Practice
Implement dictionaries in your projects.
Explore
Dive deeper into advanced dictionary techniques.
Share
Share your knowledge with others.