Dictionary
Dictionary
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ombb_lFkXfiV",
"outputId": "57f2b438-3718-4809-bd5d-b2c941566a41"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{}\n",
"<class 'dict'>\n"
]
}
],
"source": [
"a={}\n",
"print(a)\n",
"print(type(a))"
]
},
{
"cell_type": "code",
"source": [
"dict={1:\"one\",2:\"two\", 3:\"three\"}\n",
"print(dict)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ro875MVLq39Z",
"outputId": "2c603500-109d-484a-85af-ab58cc097c46"
},
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{1: 'one', 2: 'two', 3: 'three'}\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#acceses value\n",
"print(dict[1])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "_LP4ijOorAHJ",
"outputId": "801a3da7-bc3d-46e1-cd7b-e7940f8d8b1c"
},
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"one\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"print(dict[0])\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 141
},
"id": "rbPXFqTtrsH3",
"outputId": "b049fe7f-c025-484b-8184-ff3c5cb82b2c"
},
"execution_count": 6,
"outputs": [
{
"output_type": "error",
"ename": "KeyError",
"evalue": "0",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m
Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-6-04d4772449ba>\u001b[0m in \
u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \
u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdict\u001b[0m\u001b[0;34m[\
u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\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: 0"
]
}
]
},
{
"cell_type": "code",
"source": [
"# add item\n",
"dict[4]=\"four\"\n",
"print(dict)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "dDURlWvcr5sM",
"outputId": "020504a7-46ec-4927-dc78-3120868eeaab"
},
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{1: 'one', 2: 'two', 3: 'three', 4: 'four'}\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"print(dict.items())\n",
"print(dict.values())\n",
"print(dict.keys())"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1qyNSu1EsdyY",
"outputId": "16fe2f73-22c2-49d4-c7f5-1093cdd762c1"
},
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"dict_items([(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')])\n",
"dict_values(['one', 'two', 'three', 'four'])\n",
"dict_keys([1, 2, 3, 4])\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#delete of pop function\n",
"dict.pop(4)\n",
"print(dict)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "y6e81cnbtFCs",
"outputId": "3f40cc2b-1f8a-4d3d-ecc5-1e4471b79b97"
},
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{1: 'one', 2: 'two', 3: 'three'}\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#del dict[2]\n",
"\n",
"print(dict)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ec22CNDjtbFi",
"outputId": "a53d0952-e96f-4e65-a27b-e93dd96dde94"
},
"execution_count": 14,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{1: 'one', 3: 'three'}\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"dict.clear()\n",
"print(dict)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "7QtXc0IHuDOC",
"outputId": "f0b2a877-1069-4df2-aa4a-54cb78c8fe6f"
},
"execution_count": 15,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{}\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [],
"metadata": {
"id": "7ldNho2LrWhy"
}
},
{
"cell_type": "code",
"source": [
"d={\"g\":123,\"h\":353,\"r\":542}\n",
"d1={\"a\":100}\n",
"print(d)\n",
"print(d1)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1TAzsOBzuNsQ",
"outputId": "57eff005-ba8d-44b7-c5cb-f4527f50df71"
},
"execution_count": 16,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{'g': 123, 'h': 353, 'r': 542}\n",
"{'a': 100}\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"d.update(d1)\n",
"print(d)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "voaF3Utmuw70",
"outputId": "e3952569-779e-495a-aeee-a37b7915f0ba"
},
"execution_count": 17,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{'g': 123, 'h': 353, 'r': 542, 'a': 100}\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"print(d1)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "x0tqtDKsvXZP",
"outputId": "d6e1c554-d917-4367-9c3f-8b91ec4d66c2"
},
"execution_count": 18,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{'a': 100}\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "qCdTCWcWzwxc"
},
"execution_count": null,
"outputs": []
}
]
}