{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Dictionaries Operations" ] }, { "cell_type": "code", "execution_count": 72, "metadata": { "id": "yih6CE4RiIrl" }, "outputs": [], "source": [ "Dictionaries = {\"key1\":\"value1\"}" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "id": "VLkxduR8hSQ_" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}\n", "\n" ] } ], "source": [ "# Create Dictionaries\n", "dic = {\n", " \"brand\": \"Ford\",\n", " \"model\": \"Mustang\",\n", " \"year\": 1964\n", "}\n", "print(dic)\n", "print(type(dic))" ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "cOUjZZdviN0G", "outputId": "f2c4422e-453a-4f72-9bf0-f699c6339afd" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}\n" ] } ], "source": [ "dic = dict(brand = \"Ford\", model = \"Mustang\", year = 1964)\n", "print(dic)" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Pq5DQvKGkJO7", "outputId": "e5e8474c-75b8-4e59-dd4a-6c81c92901a6" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1964\n" ] } ], "source": [ "# Accessing Items\n", "x = dic[\"year\"]\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "7NrgctylkR0D", "outputId": "a1c24afa-80c7-4535-fda4-796de9986b9f" }, "outputs": [ { "data": { "text/plain": [ "'Mustang'" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = dic.get(\"model\")\n", "x" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "t9pG91qkkYDk", "outputId": "156acb1f-8197-4799-c6db-8cf4dc05a5b4" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dict_keys(['brand', 'model', 'year'])\n", "['brand', 'model', 'year']\n" ] } ], "source": [ "# keys()\n", "x = dic.keys()\n", "print(x)\n", "print(list(x))" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "XIY6AocbifhJ", "outputId": "ce5bbca4-2808-4d80-bfa2-91eb5321e8d4" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dict_values(['Ford', 'Mustang', 1964])\n", "['Ford', 'Mustang', 1964]\n" ] } ], "source": [ "# values()\n", "x = dic.values()\n", "print(x)\n", "print(list(x))" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "r3UjUOwMir1v", "outputId": "e9946a9b-d7e8-4e58-8e06-8772b0ca87ee" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])\n", "[('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)]\n" ] } ], "source": [ "# items()\n", "x = dic.items()\n", "print(x)\n", "print(list(x))" ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "P-jX07npkdWt", "outputId": "41980110-ea06-4eec-f3ab-864c32906360" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Before add: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}\n", "After add color: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'white'}\n", "After add power: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'white', 'power': '315 HP'}\n" ] } ], "source": [ "dic = {\n", " \"brand\": \"Ford\",\n", " \"model\": \"Mustang\",\n", " \"year\": 1964\n", "}\n", "print('Before add:',dic)\n", "\n", "# Add Items\n", "dic[\"color\"] = \"white\"\n", "print('After add color:',dic)\n", "\n", "d= {\"power\":\"315 HP\"}\n", "dic.update(d)\n", "print('After add power:',dic)" ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "vTI0Js9Qk4i3", "outputId": "fedbb0b2-c422-44dd-96a6-0e737cc1c601" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'white', 'power': '315 HP'}\n", "{'brand': 'Ford', 'model': 'Mustang', 'year': 2020, 'color': 'white', 'power': '400 HP'}\n" ] } ], "source": [ "dic = {\n", " 'brand': 'Ford',\n", " 'model': 'Mustang',\n", " 'year': 1964,\n", " 'color': 'white',\n", " 'power': '315 HP'\n", "}\n", "\n", "# Modify\n", "x = dic.values()\n", "\n", "print(dic) #before the change\n", "\n", "dic[\"year\"] = 2020\n", "dic[\"power\"] = '400 HP'\n", "\n", "print(dic) #after the change" ] }, { "cell_type": "markdown", "metadata": { "id": "0H-RH2KAk01h" }, "source": [ "IF condition on dictionary" ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "RIKmYHRslRkZ", "outputId": "25a74495-4db4-4dce-c157-f15ad5d609cf" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mustang\n" ] } ], "source": [ "if \"model\" in dic:\n", " print(dic[\"model\"])" ] }, { "cell_type": "markdown", "metadata": { "id": "aHCU62mgl42t" }, "source": [ "Remove" ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Tt7MiZJ4lbX6", "outputId": "fb951324-8c71-4bf4-da40-c260cbcae871" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'brand': 'Ford', 'year': 2020, 'color': 'white', 'power': '400 HP'}\n" ] } ], "source": [ "dic.pop(\"model\")\n", "print(dic)" ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "5rLw_HJGluTk", "outputId": "b0a40a0f-034b-4816-a8af-64d3b646bbb2" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'brand': 'Ford', 'year': 2020, 'color': 'white'}\n" ] } ], "source": [ "dic.popitem()\n", "print(dic)" ] }, { "cell_type": "code", "execution_count": 85, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "HXjuyg0Kl2c1", "outputId": "ebbd008f-6835-4a42-8426-54817e62e76d" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'brand': 'Ford', 'color': 'white'}\n" ] } ], "source": [ "del dic[\"year\"]\n", "print(dic)" ] }, { "cell_type": "code", "execution_count": 86, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "NaGVNcQLl_Ct", "outputId": "8f88b9c5-6606-44c4-86c9-bd0ed5d3cfb4" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{}\n" ] } ], "source": [ "dic.clear()\n", "print(dic)" ] }, { "cell_type": "markdown", "metadata": { "id": "2g18Gu0Ck-t9" }, "source": [ "for loop in dictionary" ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "id": "sjuLUjk4lMMW" }, "outputs": [], "source": [ "dic = {\n", " \"brand\": \"Ford\",\n", " \"model\": \"Mustang\",\n", " \"year\": 1964\n", "}" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "viXaYxIwlOED", "outputId": "ca313f60-0338-40df-c3be-c125fca936de" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "brand\n", "model\n", "year\n" ] } ], "source": [ "# for loop with keys\n", "for x in dic:\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "8tybG7eClb1A", "outputId": "4d3545c0-b679-4522-b26d-70364c931348" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "brand\n", "model\n", "year\n" ] } ], "source": [ "# for loop with keys\n", "for x in dic.keys():\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "hWpbaVpQmFTG", "outputId": "f7ab0e1a-2329-41ce-fb4c-747eee4605b7" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ford\n", "Mustang\n", "1964\n" ] } ], "source": [ "# for loop with values\n", "for x in dic:\n", " print(dic[x])" ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "PFYLUr8GljJA", "outputId": "b62871c8-9390-4f84-a783-0d5bfb17de3c" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ford\n", "Mustang\n", "1964\n" ] } ], "source": [ "# for loop with values\n", "for x in dic.values():\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 92, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "7NEARCAimQmX", "outputId": "ab8c1e97-025f-4619-bd8b-92731b8fdca3" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "brand : Ford\n", "model : Mustang\n", "year : 1964\n" ] } ], "source": [ "# for loop with items\n", "for x, y in dic.items():\n", " print(x,':', y)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "id": "GjgwAYbZmtFC" }, "outputs": [], "source": [ "# Nested dictionaries\n", "child1 = {\n", " \"name\" : \"Rohan\",\n", " \"year\" : 2004\n", "}\n", "child2 = {\n", " \"name\" : \"Rakib\",\n", " \"year\" : 2007\n", "}\n", "child3 = {\n", " \"name\" : \"Rana\",\n", " \"year\" : 2011\n", "}\n", "\n", "myfamily = {\n", " \"child1\" : child1,\n", " \"child2\" : child2,\n", " \"child3\" : child3\n", "}" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "28qYsQ-erbHA", "outputId": "330b9c15-a7be-45bc-a02e-970daa992ff2" }, "outputs": [ { "data": { "text/plain": [ "{'child1': {'name': 'Rohan', 'year': 2004},\n", " 'child2': {'name': 'Rakib', 'year': 2007},\n", " 'child3': {'name': 'Rana', 'year': 2011}}" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myfamily" ] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "all", "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.12.9" } }, "nbformat": 4, "nbformat_minor": 0 }