Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions comprehensions/dict/dict.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "6b76bccd",
"metadata": {},
"source": [
"# dict comprehension\n",
"\n",
"Syntax:\n",
"\n",
"```py\n",
"new_dict = {key_expression: value_expression for item in iterable}\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "a895bb05",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"id": "88fc1603",
"metadata": {},
"source": [
"## basic dict comprehension"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "d9205630",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"traditional way: {'Alice': 5, 'Bob': 3, 'Charlie': 7}\n",
"comprehension way: {'Alice': 5, 'Bob': 3, 'Charlie': 7}\n"
]
}
],
"source": [
"# Map names to their text lengths\n",
"\n",
"names = [\"Alice\", \"Bob\", \"Charlie\"]\n",
"\n",
"# Traditional way\n",
"name_lengths = {}\n",
"for name in names:\n",
" name_lengths[name] = len(name)\n",
"\n",
"print(f\"traditional way: {name_lengths}\")\n",
"\n",
"# comprehension way\n",
"name_lengths = {name: len(name) for name in names}\n",
"print(f\"comprehension way: {name_lengths}\")"
]
},
{
"cell_type": "markdown",
"id": "672cea7a",
"metadata": {},
"source": [
"## Merging Two Lists into a Dictionary (Using zip)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "daeaf009",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'id': 101, 'role': 'admin', 'status': 'active'}\n"
]
}
],
"source": [
"keys = [\"id\", \"role\", \"status\"]\n",
"values = [101, \"admin\", \"active\"]\n",
"\n",
"# comprehension way\n",
"user_profile = {k: v for k, v in zip(keys, values)}\n",
"print(user_profile)"
]
},
{
"cell_type": "markdown",
"id": "73d7be84",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"id": "f98b9ee6",
"metadata": {},
"source": [
"# Conditional"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "35b3949d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'banana': 10.8, 'mango': 18.0}\n"
]
}
],
"source": [
"prices = {\"apple\": 5, \"banana\": 12, \"mango\": 20}\n",
"\n",
"# Keep if price > 10, then apply 10% discount\n",
"discounted_price = {item: price * 0.9 for item, price in prices.items() if price > 10}\n",
"\n",
"print(discounted_price)"
]
},
{
"cell_type": "markdown",
"id": "4976a37d",
"metadata": {},
"source": [
"> Note: We use .items() to loop through both the keys and values of the original dictionary at the same time."
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv (3.14.7)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.14.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading
Loading