{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 006_dictionaries\n", "\n", "[Source](https://github.com/iArunava/Python-TheNoTheoryGuide/)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Simple Dictionary\n", "# Dictionary allows to have key:value pairs\n", "d1 = {\"Jennifer\":8, 'A':65, 66:'B', 9.45:\"Decimals\"}\n", "\n", "print (d1[\"Jennifer\"])\n", "print (d1['A'])\n", "print (d1[66])\n", "print (d1[9.45])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Adding new kay:value pairs\n", "d1 = {\"Jennifer\":8, 'A':65, 66:'B', 9.45:\"Decimals\"}\n", "\n", "d1[\"Scarlett\"] = 8\n", "d1[7.56] = \"Is a decimal!\"\n", "d1['Q'] = 17\n", "\n", "print (d1[\"Scarlett\"])\n", "print (d1[7.56])\n", "print (d1['Q'])\n", "\n", "print (d1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Declaring an empty dictionary\n", "d1 = {}\n", "\n", "# Add new values\n", "d1[\"Jennifer\"] = \"Python\"\n", "d1[\"Scarlett\"] = \"Python\"\n", "d1[45] = 56\n", "\n", "print (d1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Modifying values in a dictionary\n", "d1 = {\"Python\":\"Is a language\", \"Jennifer\":\"Feels like a supergirl with Python\"}\n", "\n", "d1[\"Python\"] = \"Is Love\"\n", "d1[\"Jennifer\"] = 8\n", "print (d1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Removing Key:Value pairs\n", "d1 = {\"Key\":\"Value\", \"Jennifer\":\"Scarlett\", \"Scarlett\":\"Jennifer\"}\n", "\n", "del d1[\"Key\"]\n", "print (d1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Storing a dictionary inside a dictionary\n", "d1 = {'A':65, 'B':66, 'C':67, 'D': {\n", " \"Breaking\": \"Dict into dicts\", \n", " \"Inception\": \"All over\", \n", " '!': \"XD\"}, \n", " 'E':69, \n", " \"What happened with D?\": \"It became a 'D'ictionary! XD\",\n", " 66:66}\n", "\n", "print (d1['D'][\"Inception\"])\n", "print (d1[\"What happened with D?\"])\n", "print ('\\n')\n", "print (d1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Looping through a dictionary\n", "for key, value in d1.items():\n", " print(\"Key: \" + str(key))\n", " print (\"Value: \" + str(value))\n", " print('\\n')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Looping through the keys in dictionary\n", "for key in d1.keys():\n", " print(\"Key: \" + str(key))\n", " print (\"Value: \" + str(d1[key]))\n", " print('\\n')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Note: Default behaviour is to loop through keys if not specified d1.keys()\n", "for k in d1:\n", " print(\"Key: \" + str(k))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Check if a particular key is present\n", "if 'F' not in d1.keys():\n", " print (\"What happened to F?\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Looping through dictonary keys in sorted order (increasing)\n", "for key in sorted(d1.keys()):\n", " print (\"Key :\" + key + '\\t' + \"Value :\" + str(d1[key]) + '\\n')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Looping through dictonary keys in sorted order (decreasing)\n", "for key in sorted(d1.keys(), reverse=True):\n", " print (\"Key :\" + key + '\\t' + \"Value :\" + str(d1[key]) + '\\n')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Looping through values in dictionary (with repeats)\n", "# Note: If two or more keys have the same value then this method will print all of them\n", "for value in d1.values():\n", " print (str(value).title())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# List in Dictionary\n", "d1 = {\"l1\":['A', 'B', 'C', 'D'],\n", " \"l2\":['E', 'F', 'G', 'H'],\n", " 45 : \"qwerty\",\n", " '$' : \"$Dollar$\"}\n", "\n", "# Accessing the elements in list\n", "print (d1[\"l1\"][2])\n", "print (d1[\"l2\"][-1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Looping over just the lists in dictionary\n", "for k in d1.keys():\n", " if type(d1[k]) == list:\n", " print (\"List :\" + k)\n", " for val in d1[k]:\n", " print (val)\n", " print ('\\n')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }