{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionary basics" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Key': 'value'}\n" ] } ], "source": [ "# Insering an element inside a dictionary\n", "mydict = dict()\n", "mydict[\"Key\"] = \"value\"\n", "print(mydict)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "France 1\n", "Apple 2\n", "Banana 3\n" ] } ], "source": [ "# Traversing through a dictionary\n", "mydict = {\"France\" : 1, \"Apple\": 2, \"Banana\": 3}\n", "for key, value in mydict.items():\n", " print(key, value)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Apple': 2, 'Banana': 3}\n" ] } ], "source": [ "# Deleting an element from a dictionary\n", "mydict = {\"France\" : 1, \"Apple\": 2, \"Banana\": 3}\n", "mydict.pop(\"France\")\n", "print(mydict)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Finding an element in a dictionary\n", "mydict = {\"France\" : 1, \"Apple\": 2, \"Banana\": 3}\n", "mydict.get(\"France\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{}\n" ] } ], "source": [ "# Deleting all the elements from a dictionary\n", "mydict = {\"France\" : 1, \"Apple\": 2, \"Banana\": 3}\n", "mydict.clear()\n", "print(mydict)" ] } ], "metadata": { "interpreter": { "hash": "40d3a090f54c6569ab1632332b64b2c03c39dcf918b08424e98f38b5ae0af88f" }, "kernelspec": { "display_name": "Python 3.8.3 ('base')", "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.8.3" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }