{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "Dictionary.ipynb", "version": "0.3.2", "provenance": [], "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "C8zY2j4Ai_ui", "colab_type": "text" }, "source": [ "# Python Dictionary from Scratch!!!" ] }, { "cell_type": "markdown", "metadata": { "id": "xVfVg95wjCdY", "colab_type": "text" }, "source": [ "## Let us understand the fundamentals of dictionaries in python." ] }, { "cell_type": "markdown", "metadata": { "id": "t1JChAljjD9Z", "colab_type": "text" }, "source": [ "![alt text](https://www.learntek.org/blog/wp-content/uploads/2018/07/PYTHON-FOR-LOOPS-1.png)" ] }, { "cell_type": "markdown", "metadata": { "id": "zCwE8lv4jIvv", "colab_type": "text" }, "source": [ "Dictionary in python comprises an unordered collection of data stored in a key: value format. Dictionary is also known as \"Associative memories\" or \"Associative arrays\". In a dictionary, the key must always be unique. Whereas the values can be repeated and can be of any data type. The key-value pair should be represented as \"key: value\" (: colon is a must). Shown below are some most used dictionary methods and are one of the most commonly asked interview questions." ] }, { "cell_type": "markdown", "metadata": { "id": "vrBc0WN-jK69", "colab_type": "text" }, "source": [ "**For example, declaring a dictionary in python:**" ] }, { "cell_type": "code", "metadata": { "id": "9tX_rrR8jQIx", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "7eeda567-e6f9-4fed-8462-b95182f0d8ef" }, "source": [ "# Declaring a dictionary\n", "\n", "dictionary = {}\n", "dictionary = {'Name': 'Tanu', 'Sex': 'Male', 'Age': 23}\n", "print(dictionary)" ], "execution_count": 85, "outputs": [ { "output_type": "stream", "text": [ "{'Name': 'Tanu', 'Sex': 'Male', 'Age': 23}\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "-r9P3fctjSgg", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "15bee850-9297-4a5b-a75c-57f57aab7e87" }, "source": [ "print(type(dictionary))" ], "execution_count": 86, "outputs": [ { "output_type": "stream", "text": [ "\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "vIfN4FqVjUBD", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "CqpWeQ3ojWyC", "colab_type": "text" }, "source": [ "# 1) Accessing the elements in a dictionary\n", "\n", "Dictionaries can be accessed by their index to get the value. To do this, all you have to do is just place the number or the name of the key inside the pair of square brackets along with the name of the dictionary.\n" ] }, { "cell_type": "code", "metadata": { "id": "mnW2atajjV87", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "e154041c-97f8-41af-aee5-c6bc582d3ac2" }, "source": [ "# Accessing the elements in a dictionary\n", "\n", "dictionary = {'Name': 'Tanu', 'Sex': 'Male', 'Age': 23}\n", "print(dictionary)" ], "execution_count": 87, "outputs": [ { "output_type": "stream", "text": [ "{'Name': 'Tanu', 'Sex': 'Male', 'Age': 23}\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "a_X0bjTOjcei", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "67e1229a-1354-4b5f-d575-3a85e48ff5b4" }, "source": [ "print(dictionary['Name'])" ], "execution_count": 88, "outputs": [ { "output_type": "stream", "text": [ "Tanu\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "yhlDznhqjelc", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "1ed76bf2-7b1a-4b8c-d6ec-a8309cc3a5ac" }, "source": [ "print(dictionary['Sex'])" ], "execution_count": 89, "outputs": [ { "output_type": "stream", "text": [ "Male\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "C6t-3wqojv4e", "colab_type": "text" }, "source": [ "If you try to access the key which is not declared in the dictionary then an exception would be raised indicating no results found:" ] }, { "cell_type": "code", "metadata": { "id": "WzNJMc1yji5U", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 166 }, "outputId": "ad52059f-edff-44b1-9e45-c84c4577b7e4" }, "source": [ "dictionary['Height']" ], "execution_count": 42, "outputs": [ { "output_type": "error", "ename": "KeyError", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdictionary\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Height'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mKeyError\u001b[0m: 'Height'" ] } ] }, { "cell_type": "markdown", "metadata": { "id": "uUDemRUzjzjj", "colab_type": "text" }, "source": [ "Also, we can use the get() of the dictionary to get or access the values of the dictionary as seen below:" ] }, { "cell_type": "code", "metadata": { "id": "1oV7TTuDj0AM", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "e0364d85-972e-453f-c098-3c573bb9692b" }, "source": [ "print(dictionary.get('Age'))" ], "execution_count": 90, "outputs": [ { "output_type": "stream", "text": [ "23\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "hpq17gxsj7tR", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "ZGLoW99Mj8gJ", "colab_type": "text" }, "source": [ "# 2) Removing the elements from a dictionary.\n", "\n", "In python, there are four different ways or methods that can be used to remove an element from a dictionary. Those are pop, del, popitem and clear. The del is used to delete a key from a dictionary, the pop is used to remove a specific key from a dictionary, popitem is used to remove the key arbitrarily from a dictionary, and the clear is used to just clear or delete all the elements in the dictionary (as the name suggests)." ] }, { "cell_type": "code", "metadata": { "id": "WVSsS_dXj2z4", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "b1431527-a015-45d8-f536-74e2440b6532" }, "source": [ "# Removing the elements from a dictionary\n", "\n", "dictionary = {'Name': 'Tanu', 'Sex': 'Male', 'Age': 23, 'Height': 5.8, 'Occupation': 'Student'}\n", "print(dictionary)" ], "execution_count": 55, "outputs": [ { "output_type": "stream", "text": [ "{'Name': 'Tanu', 'Sex': 'Male', 'Age': 23, 'Height': 5.8, 'Occupation': 'Student'}\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "NvQAuY1NkCj-", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "ac29a917-91a3-44d5-d9bc-0a50fc947657" }, "source": [ "# Deleting an element\n", "\n", "del dictionary['Name']\n", "print(dictionary)" ], "execution_count": 56, "outputs": [ { "output_type": "stream", "text": [ "{'Sex': 'Male', 'Age': 23, 'Height': 5.8, 'Occupation': 'Student'}\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "C1rKkMWWkm-p", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "8c44a1f4-8ae0-4bdc-be02-75044b6524d7" }, "source": [ "# Popping an item\n", "\n", "print(dictionary.popitem())" ], "execution_count": 57, "outputs": [ { "output_type": "stream", "text": [ "('Occupation', 'Student')\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "thMP9vUOk5i1", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "dc048c74-968c-4a85-9cc2-30f76c03d4e5" }, "source": [ "# Popping the value\n", "\n", "print(dictionary.pop('Sex'))" ], "execution_count": 58, "outputs": [ { "output_type": "stream", "text": [ "Male\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "BvkNptWdlMFX", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "21faab88-2ae4-4dca-d0de-afc0d1439603" }, "source": [ "print(dictionary)" ], "execution_count": 59, "outputs": [ { "output_type": "stream", "text": [ "{'Age': 23, 'Height': 5.8}\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "D1yvCB31liJt", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "7c2ff369-d91f-4e94-e9a8-8f3b03cd1412" }, "source": [ "# Clearing the entire dictionary\n", "\n", "dictionary.clear()\n", "print(dictionary)" ], "execution_count": 60, "outputs": [ { "output_type": "stream", "text": [ "{}\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "JV0-gA9Sl6GM", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Xs1b4dQGl6wB", "colab_type": "text" }, "source": [ "# 3) Iterating Through a Dictionary\n", "\n", "Using a for loop we can iterate through each key in a dictionary as shown below:" ] }, { "cell_type": "code", "metadata": { "id": "mgqztEpnmGNx", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "04dcf27c-2e41-4a21-878a-a33856f75f72" }, "source": [ "dictionary = {'Name': 'Tanu', 'Sex': 'Male', 'Age': 23, 'Height': 5.8, 'Occupation': 'Student'}\n", "print(dictionary)" ], "execution_count": 91, "outputs": [ { "output_type": "stream", "text": [ "{'Name': 'Tanu', 'Sex': 'Male', 'Age': 23, 'Height': 5.8, 'Occupation': 'Student'}\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "r0kYERvMmNtc", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 104 }, "outputId": "92753acf-07e1-475e-f6ee-b8fa82cf0045" }, "source": [ "# Iterating through the values\n", "\n", "for i in dictionary:\n", " print(dictionary[i])" ], "execution_count": 62, "outputs": [ { "output_type": "stream", "text": [ "Tanu\n", "Male\n", "23\n", "5.8\n", "Student\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "aV9J2J96mRLU", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 104 }, "outputId": "dbdc2442-e1db-447f-b283-ddc17ad3ffcb" }, "source": [ "# Iterating through the keys\n", "\n", "for i in dictionary:\n", " print(i)" ], "execution_count": 63, "outputs": [ { "output_type": "stream", "text": [ "Name\n", "Sex\n", "Age\n", "Height\n", "Occupation\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "MqxSw4n5mTte", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "4KvR9r3bmUPO", "colab_type": "text" }, "source": [ "# 4) All\n", "\n", "The all () in dictionary returns \"True\" if all keys of the dictionary are true (or if the dictionary is empty). If the keys in the dictionary are true or if the dictionary all method returns true or else it will return false." ] }, { "cell_type": "code", "metadata": { "id": "ISO7NPhimWs8", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "6109bba0-b72a-4dd1-fb05-2bd85809ae6d" }, "source": [ "dictionary = {0: \"Tanu\", 1: \"Prabhu\"}\n", "print(dictionary)" ], "execution_count": 64, "outputs": [ { "output_type": "stream", "text": [ "{0: 'Tanu', 1: 'Prabhu'}\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "6Y9UwtAImag9", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "478d7a6e-4872-480f-b85b-0700138a6d1a" }, "source": [ "print(all(dictionary))" ], "execution_count": 65, "outputs": [ { "output_type": "stream", "text": [ "False\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "OLjFMuKFmcxS", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "182db9c8-5c84-4057-a133-695bf9336e70" }, "source": [ "dictionary = {1: \"Tanu\", 1: \"Prabhu\"}\n", "print(all(dictionary))" ], "execution_count": 66, "outputs": [ { "output_type": "stream", "text": [ "True\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "JpjaiflcmfJm", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "ab3c5db9-eab7-4224-81ef-c030809b7c36" }, "source": [ "dictionary = {}\n", "print(all(dictionary))" ], "execution_count": 67, "outputs": [ { "output_type": "stream", "text": [ "True\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "Xqj6a3cBmiXR", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "K7AJYZLumi-H", "colab_type": "text" }, "source": [ "# 5) Any\n", "The any () in dictionary return \"True\" if any key of the dictionary is true. If the dictionary is empty, returns \"False\"." ] }, { "cell_type": "code", "metadata": { "id": "hMS8EF6GmlO8", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "24b705e3-8722-433d-f01e-e5271cdd58fd" }, "source": [ "dictionary = {0: \"Tanu\", 1: \"Prabhu\"}\n", "print(dictionary)" ], "execution_count": 68, "outputs": [ { "output_type": "stream", "text": [ "{0: 'Tanu', 1: 'Prabhu'}\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "CLBhmNVImqKL", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "350f8f61-8c1c-42ca-a1e1-f5df2bfecd54" }, "source": [ "print(any(dictionary))" ], "execution_count": 69, "outputs": [ { "output_type": "stream", "text": [ "True\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "aNXU7SVJmr9j", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "f78ced6c-80fa-40fb-f110-7b2e8ff872cf" }, "source": [ "dictionary = {0: \"Tanu\", 0: \"Prabhu\"}\n", "print(any(dictionary))" ], "execution_count": 70, "outputs": [ { "output_type": "stream", "text": [ "False\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "AZMYfpd2muSl", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "f26a211e-854c-4c9c-b2a5-827d96c489e4" }, "source": [ "# 0 is False\n", "\n", "dictionary = {0: \"Tanu\"}\n", "print(any(dictionary))" ], "execution_count": 71, "outputs": [ { "output_type": "stream", "text": [ "False\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "LLmp_D8ZmwLU", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "dqpHHnLYmwpT", "colab_type": "text" }, "source": [ "# 6) Len\n", "The length () method in the dictionary returns the length of the dictionary (obviously). It returns the number of items of the dictionary." ] }, { "cell_type": "code", "metadata": { "id": "JhXR7ZxrmycL", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "298707af-2b19-4471-f093-f6d8325bda42" }, "source": [ "dictionary = {'Name': 'Tanu', 'Sex': 'Male', 'Age': 23, 'Height': 5.8, 'Occupation': 'Student'}\n", "print(dictionary)" ], "execution_count": 92, "outputs": [ { "output_type": "stream", "text": [ "{'Name': 'Tanu', 'Sex': 'Male', 'Age': 23, 'Height': 5.8, 'Occupation': 'Student'}\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "60RU8jVKm4V5", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "fc8fdf41-1e8c-4fc3-a262-186365656ff0" }, "source": [ "print(len(dictionary))" ], "execution_count": 73, "outputs": [ { "output_type": "stream", "text": [ "5\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "jcVXh7ijm5dU", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "ciqeNRdum7nS", "colab_type": "text" }, "source": [ "# 7) Sorted\n", "The Sorted() method in dictionary returns a new sorted list of keys in the dictionary." ] }, { "cell_type": "code", "metadata": { "id": "kEBy3Bp1nHfZ", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "36457171-1fb7-4b67-ec64-e35aac9a627d" }, "source": [ "dictionary = {'Name': 'Tanu', 'Sex': 'Male', 'Age': 23, 'Height': 5.8, 'Occupation': 'Student'}\n", "print(dictionary)" ], "execution_count": 74, "outputs": [ { "output_type": "stream", "text": [ "{'Name': 'Tanu', 'Sex': 'Male', 'Age': 23, 'Height': 5.8, 'Occupation': 'Student'}\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "gDzRnbZznFkt", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "2e792f1d-170a-4ffc-d161-73d7cde14fac" }, "source": [ "# Sorting in a ascending order\n", "\n", "print(sorted(dictionary))" ], "execution_count": 75, "outputs": [ { "output_type": "stream", "text": [ "['Age', 'Height', 'Name', 'Occupation', 'Sex']\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "6ZGg4dPhnKVV", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "8bc5d01c-920d-4c54-8a43-0188358f9c2d" }, "source": [ "# Sorting in a descending order\n", "\n", "print(sorted(dictionary, reverse = True))" ], "execution_count": 76, "outputs": [ { "output_type": "stream", "text": [ "['Sex', 'Occupation', 'Name', 'Height', 'Age']\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "8LBfLFhinMHY", "colab_type": "text" }, "source": [ "The sorted() method accepts a reverse parameter as an optional argument.\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "uPt_rmNBnNb-", "colab_type": "text" }, "source": [ "# 8) Copy\n", "As the name suggests the copy method in dictionary returns a copy of the dictionary." ] }, { "cell_type": "code", "metadata": { "id": "udFUMrvcnXgx", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "cd4a8ccf-3041-4b72-af92-00abebf166f1" }, "source": [ "dictionary = {'Name': 'Tanu', 'Sex': 'Male', 'Age': 23, 'Height': 5.8, 'Occupation': 'Student'}\n", "print(dictionary)" ], "execution_count": 77, "outputs": [ { "output_type": "stream", "text": [ "{'Name': 'Tanu', 'Sex': 'Male', 'Age': 23, 'Height': 5.8, 'Occupation': 'Student'}\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "KhKgB1yNnYW1", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "0e5573ac-0ab4-44c5-e56b-fb2394aed33b" }, "source": [ "dictionary1 = {}\n", "print(dictionary1)" ], "execution_count": 78, "outputs": [ { "output_type": "stream", "text": [ "{}\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "YOVHVyEinaj2", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "2911442c-87c1-4e53-a7df-2b94696489a8" }, "source": [ "dictionary1 = dictionary.copy()\n", "print(dictionary1)" ], "execution_count": 80, "outputs": [ { "output_type": "stream", "text": [ "{'Name': 'Tanu', 'Sex': 'Male', 'Age': 23, 'Height': 5.8, 'Occupation': 'Student'}\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "GrMN6BLJnh95", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "lOTpUM0Enk4N", "colab_type": "text" }, "source": [ "# 9) Keys\n", "Python dictionary method key() returns a list of all the available keys in the dictionary." ] }, { "cell_type": "code", "metadata": { "id": "2U_OwjIqnknh", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "2fd1644b-5b46-4049-a529-0c46f0f60861" }, "source": [ "dictionary = {'Name': 'Tanu', 'Sex': 'Male', 'Age': 23, 'Height': 5.8, 'Occupation': 'Student'}\n", "print(dictionary)" ], "execution_count": 81, "outputs": [ { "output_type": "stream", "text": [ "{'Name': 'Tanu', 'Sex': 'Male', 'Age': 23, 'Height': 5.8, 'Occupation': 'Student'}\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "NP7u5dy-nfN8", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "d59d2e64-73f5-4b9a-8cd6-6540b46ca0d2" }, "source": [ "print(dictionary.keys())" ], "execution_count": 93, "outputs": [ { "output_type": "stream", "text": [ "dict_keys(['Name', 'Sex', 'Age', 'Height', 'Occupation'])\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "BWvmAH_GntfA", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "HAVoI3_bnuMx", "colab_type": "text" }, "source": [ "# 10) Values\n", "Python dictionary method values() returns a list of all the values available in a given dictionary." ] }, { "cell_type": "code", "metadata": { "id": "qXjjhFtgny0I", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "5a9efc5a-d79d-4ceb-aa15-0e428f76059b" }, "source": [ "dictionary = {'Name': 'Tanu', 'Sex': 'Male', 'Age': 23, 'Height': 5.8, 'Occupation': 'Student'}\n", "print(dictionary)" ], "execution_count": 83, "outputs": [ { "output_type": "stream", "text": [ "{'Name': 'Tanu', 'Sex': 'Male', 'Age': 23, 'Height': 5.8, 'Occupation': 'Student'}\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "292VDOpfn0kt", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "dfd04120-f03c-4aee-83d2-b9ad217a5a0e" }, "source": [ "print(dictionary.values())" ], "execution_count": 84, "outputs": [ { "output_type": "stream", "text": [ "dict_values(['Tanu', 'Male', 23, 5.8, 'Student'])\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "oHxpl4r1n2H6", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "U-TThaF4n2rN", "colab_type": "text" }, "source": [ "Hence above are the very important techniques or functions of Dictionary in Python. Some of the examples were referred from [Python Dictionary](https://www.programiz.com/python-programming/dictionary). I have written this tutorial in a simple way such that everybody can understand and master the concepts of Dictionaries in Python without prior programming knowledge or experience. If you guys have some doubts in the code, the comment section is all yours. Thank you have a good day." ] } ] }