Formation 90 min read Data Structures

πŸ“¦ Dictionaries in Python

Python & Data Science Chapter : Data Structures Sub-chapter : Dictionaries

Learning objectives

🎯 Objectives:\n
1Master dictionaries\n2. Use defaultdict\n3. Use Counter\n4. Understand comprehensions

Introduction

πŸ“– Dictionaries are key-value pair collections.

Theoretical content

Dictionary:\n
PYTHON
\nmy_dict = {"name": "Dupont", "age": 25}\n

Practical examples

πŸ’» Example: Phone directory\n
PYTHON
\ndirectory = {}\ndirectory["Jean"] = "123456789"\n

Best practices

1Use get() instead of []\nβœ… 2. Use defaultdict\nβœ… 3. Use Counter for counting

Common pitfalls

Accessing non-existent key\n
Use get(): value = dict.get("key")

Summary

Dictionaries: key-value pairs\nβœ… get(): safe access\nβœ… defaultdict: default values\nβœ… Counter: counting elements

Additional resources

πŸ“š docs.python.org/3/tutorial/datastructures.html#dictionaries