{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "Lists.ipynb", "provenance": [], "collapsed_sections": [], "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": "HQpjSZYzSZI4", "colab_type": "text" }, "source": [ "# Python Lists from Scratch !!!" ] }, { "cell_type": "markdown", "metadata": { "id": "VS9zKh3lo7si", "colab_type": "text" }, "source": [ "## Let's understand the Python Lists from basic to advance level." ] }, { "cell_type": "markdown", "metadata": { "id": "cN68OuxdpELP", "colab_type": "text" }, "source": [ "![alt text](https://lh3.googleusercontent.com/ge8A9HL1KBFhclU688B6ZTS7emC_yhZ_X6GWhlHWatiI8g3PQMKcUP9UKjRnUg3lolaNmCT-1vcuHX8nok6ju2JU05l364fgtHEI8EsPGGXT_lWfUTyfIkB6mOvcXy9itWxJneDcf1ordedYfYdjbCoGNbodCaVGRGtzT3tNmjUMmorxarkVkm0sSLv1LuCq272yUr1aOkwClVXc3SaDZeZ7sQ6K5L3uMQbopjjaaIv4EVu7Si-9ZfQrUk23kvWEroV527WWduw8ZBwPAW_ESdxEmlgoV2S0kFU5g2GfDUeJbZvzqXMJV6YjXUrxWddq7NceOlbKnBs3a5wSkyvmiR63YYz61pn2oVm6Vs4eAibWWQUwMpsakVmqfWRPiQ2-YxC-F6wutNhPYkqY1FLQZqFHHoPoNnJY1rbsZPV4aOnmxB4XKQqXSTfA3JEKBtmk5pIc8aL2k8IvrGfHtt0eTloYd6FVpRrKT0Nf2IQ1Fffp7_PNJOXE3egbjxQ1xaA3aAS0PB5EnmLRW3sQRdEZFt3s519papGi-eNJn50z1mL7NLKxo19J2WL3j_85tMPy8iLzL6kjQenR220Awaq9vlIR7FyUY0694T4OfFY50KOH1mDw0OOBt-sZFcMuKOCE-nAgXC_cPeHcZwRABYQ6T-BihIrnPqdhdTA82bZPKoZlG1jkcGSRnU4=w600-h320-no)" ] }, { "cell_type": "markdown", "metadata": { "id": "q-UYWzQjSAoK", "colab_type": "text" }, "source": [ "The list is a data structure in Python which acts as a container to hold or store multiple data at the same time. Lists are mutable or changeable and ordered sequence of elements. To know more about Python Lists you can visit the official Python Lists documentation. One thing you have to keep in mind is that often we must declare lists using [ ] brackets. The elements inside the [ ] are the values of the list. For example:" ] }, { "cell_type": "code", "metadata": { "id": "R98_K5UQSx9R", "colab_type": "code", "outputId": "a1173a9c-1368-42de-dea8-39b206472095", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Creating an empty list called \"num\"\n", "num = [ ]\n", "# Adding the values inside the list\n", "num = [ 1, 2, 3, 4, 5]\n", "# Printing the list\n", "num" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "metadata": { "tags": [] }, "execution_count": 1 } ] }, { "cell_type": "markdown", "metadata": { "id": "D3L271SSS3vL", "colab_type": "text" }, "source": [ "Here the name of the list is \"num\" and the list values are 1, 2, 3, 4, 5. The advantages of lists are the values inside the lists need not be of the same type meaning:" ] }, { "cell_type": "code", "metadata": { "id": "EvnNwkDxTIJh", "colab_type": "code", "outputId": "0c249813-fb4c-406d-9719-970cba768a0a", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Adding the values irrespective of their data type: Integer, String, float.\n", "num = [1, 2, 3, \"Tanu\", 4.0, 4/2]\n", "num" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[1, 2, 3, 'Tanu', 4.0, 2.0]" ] }, "metadata": { "tags": [] }, "execution_count": 2 } ] }, { "cell_type": "markdown", "metadata": { "id": "M55H22taVBGs", "colab_type": "text" }, "source": [ "In order to check the variable if it is a list or not, use the \"type\" method as follows:" ] }, { "cell_type": "code", "metadata": { "id": "dTPnn1k2Vd4T", "colab_type": "code", "outputId": "976bc5da-02da-46b7-8962-8d7141cc73be", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Create a list as num\n", "num = [1, 2, 3, 4, 5]\n", "# Use type method by passing the name of the list as an arguement \n", "type(num)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "list" ] }, "metadata": { "tags": [] }, "execution_count": 3 } ] }, { "cell_type": "markdown", "metadata": { "id": "vAWAY_5jTMNp", "colab_type": "text" }, "source": [ "### 1) Creating, accessing and calculating the length of a list." ] }, { "cell_type": "markdown", "metadata": { "id": "65IJ_gGkps7G", "colab_type": "text" }, "source": [ "Let us create a list called \"name\" and then insert some values, later we can then access the elements inside the list using the index with the help of [ ] by placing the index values inside it. We can then calculate the length of the list by using the len ( ) method, just pass the name of the list as an argument to the len( ) method, finally we can also get the length of the individual elements inside the list by using the same len ( ) method but this time we should specify the index position as an argument." ] }, { "cell_type": "code", "metadata": { "id": "c1s1azbMp10C", "colab_type": "code", "outputId": "e75513e2-809f-4485-8e15-ea0e1ad05951", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Creating a list called name\n", "name = [\"Tanu\", \"Nanda\", \"Prabhu\"]\n", "name" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Tanu', 'Nanda', 'Prabhu']" ] }, "metadata": { "tags": [] }, "execution_count": 4 } ] }, { "cell_type": "code", "metadata": { "id": "6m96qFvMp4ii", "colab_type": "code", "outputId": "cdff1217-70af-441e-faa3-2d87ce7660ff", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Accessing the elements in the list\n", "name[0] # Tanu" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Tanu'" ] }, "metadata": { "tags": [] }, "execution_count": 5 } ] }, { "cell_type": "code", "metadata": { "id": "21JdFTwEp8TP", "colab_type": "code", "outputId": "6cbe10d5-634f-4b61-826b-c84d64c78156", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Calculating the length of the list.\n", "len(name)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "3" ] }, "metadata": { "tags": [] }, "execution_count": 6 } ] }, { "cell_type": "code", "metadata": { "id": "L8c60jFvp_OZ", "colab_type": "code", "outputId": "0aa2d32a-c970-4d62-cad9-c7224f805004", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Calculating the length of individual elements in a list\n", "len(name[0]) # length of \"Tanu\" is 4" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "4" ] }, "metadata": { "tags": [] }, "execution_count": 7 } ] }, { "cell_type": "markdown", "metadata": { "id": "CeaPhseIqCHD", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "H8Oom4IsfhEz", "colab_type": "text" }, "source": [ "### 2) Assignment operator on Lists" ] }, { "cell_type": "markdown", "metadata": { "id": "upErO4CAqHtF", "colab_type": "text" }, "source": [ "Assignment with a \"=\" on lists does not make a copy. Instead, the assignment makes the two variables point to the one list in the memory. Sometimes an assignment operator can be used to copy from one list to the other." ] }, { "cell_type": "code", "metadata": { "id": "NfQYYrcAUjTP", "colab_type": "code", "outputId": "8860d7a7-3e35-401a-fc64-30993240725f", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Creating a list called name\n", "name = [\"Tanu\", \"Nanda\", \"Prabhu\"]\n", "name" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Tanu', 'Nanda', 'Prabhu']" ] }, "metadata": { "tags": [] }, "execution_count": 8 } ] }, { "cell_type": "code", "metadata": { "id": "PGLdY-RqqOJO", "colab_type": "code", "outputId": "309525ba-f2ff-4b4d-8ade-ff3b857e49ed", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Creating an empty list names\n", "names = []\n", "names" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[]" ] }, "metadata": { "tags": [] }, "execution_count": 9 } ] }, { "cell_type": "code", "metadata": { "id": "QXjv7vx2qRBE", "colab_type": "code", "outputId": "17fa500e-2253-4f64-a68a-c2b08a0c9b68", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Using assignment operator on Lists doesn't create a copy.\n", "names = name\n", "# Assigning the old list name to the new list names.\n", "names" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Tanu', 'Nanda', 'Prabhu']" ] }, "metadata": { "tags": [] }, "execution_count": 10 } ] }, { "cell_type": "markdown", "metadata": { "id": "lMHzgaNcqUAG", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "LjZeunKZAsNR", "colab_type": "text" }, "source": [ "### 3) Appending two lists to a single list." ] }, { "cell_type": "markdown", "metadata": { "id": "QJzMRZ7lqamh", "colab_type": "text" }, "source": [ "Usually appending two lists can be done by using append( ) method, but appending can also be done by using a '+' (here + doesn't mean addition) whereas + can be used to add or merge two lists into a single list as shown below." ] }, { "cell_type": "code", "metadata": { "id": "ryhZnCeNq3Bl", "colab_type": "code", "outputId": "291afc18-a44d-4fb0-8fb5-fa3e51467068", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Creating a new list called Cars\n", "cars = [\"Mercedes\", \"BMW\", \"Audi\"]\n", "cars" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Mercedes', 'BMW', 'Audi']" ] }, "metadata": { "tags": [] }, "execution_count": 11 } ] }, { "cell_type": "code", "metadata": { "id": "1Cwsr4N2q6X2", "colab_type": "code", "outputId": "2f9f26ae-847a-4fac-d164-c64550778b33", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Creating a new list called bikes\n", "bikes = [\"Honda\", \"Yamaha\", \"Aprilla\"]\n", "bikes" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Honda', 'Yamaha', 'Aprilla']" ] }, "metadata": { "tags": [] }, "execution_count": 12 } ] }, { "cell_type": "code", "metadata": { "id": "JQCkUSgRq9d1", "colab_type": "code", "outputId": "f383f572-cd2e-40ab-89d1-986aa3bf4d2a", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Appending both the lists\n", "cars_bikes = cars + bikes\n", "cars_bikes" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Mercedes', 'BMW', 'Audi', 'Honda', 'Yamaha', 'Aprilla']" ] }, "metadata": { "tags": [] }, "execution_count": 13 } ] }, { "cell_type": "markdown", "metadata": { "id": "QfvPXI4qrA-O", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "sK4z2XxqrBrT", "colab_type": "text" }, "source": [ "### 4) Using FOR and IN in lists" ] }, { "cell_type": "markdown", "metadata": { "id": "iIziYcNkrEhZ", "colab_type": "text" }, "source": [ "In Python the \"for\" and \"in\" are called constructs, these constructs are easy to use, they are used whenever you need to iterate over a list." ] }, { "cell_type": "code", "metadata": { "id": "VagFTTI8rF92", "colab_type": "code", "outputId": "3e6969c6-1955-47c0-86bc-e073c860f4f4", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Creating a list\n", "num = [1, 2, 3, 4, 5]\n", "# Assigning sum to 0\n", "sum = 0\n", "# Using a for loop to iterate over the list\n", "for i in num:\n", " sum = sum + i\n", "print(sum) # 15" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "15\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "sAGKvOUJrIxS", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "HGe3maLyrJb-", "colab_type": "text" }, "source": [ "### 5) Using IF and IN in lists." ] }, { "cell_type": "markdown", "metadata": { "id": "1wYSVbejrLYE", "colab_type": "text" }, "source": [ "The \"if \" and \"in\" construct on its own is an easy way to test if an element appears in a list (or other collection), it tests if the value is in the collection, returning True/False. This also works for the string, characters in Python." ] }, { "cell_type": "code", "metadata": { "id": "ISmvPEvGrNCi", "colab_type": "code", "outputId": "d9e289ed-9604-40c2-d28a-cd188ef85ba3", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Creating a list\n", "num = [1, 2, 3, 4, 5]\n", "if 1 in num:\n", " print(\"True\")\n", "else:\n", " print(\"False\")" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "True\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "pam8Jo6orOsR", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "ASBVmHKdrPQT", "colab_type": "text" }, "source": [ "### 6) Range function in Python." ] }, { "cell_type": "markdown", "metadata": { "id": "kxLXRIPyrSrB", "colab_type": "text" }, "source": [ "The Range function in Python is used as a boundary in the looping constructs, the range function starts from 0 to n-1, it doesn't include the last number as shown below." ] }, { "cell_type": "code", "metadata": { "id": "eTslCEJnrTR2", "colab_type": "code", "outputId": "2d1571e1-2243-4b08-c96d-b39f9b8496cf", "colab": { "base_uri": "https://localhost:8080/", "height": 104 } }, "source": [ "# Range starts 0 to n-1\n", "for i in range(5):\n", " print(i)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "ey7oKcNjrVlx", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "tmECTrCqrYcn", "colab_type": "text" }, "source": [ "### 7) While loop in python" ] }, { "cell_type": "markdown", "metadata": { "id": "rLI-Gxovrasz", "colab_type": "text" }, "source": [ "Python has a standard while-loop which works similar to the For loop, first you have to initialize, then insert a condition and finally increment" ] }, { "cell_type": "code", "metadata": { "id": "CmUGkdqtrWRC", "colab_type": "code", "outputId": "4212904e-b61e-47f5-ee65-a565fb70aa84", "colab": { "base_uri": "https://localhost:8080/", "height": 191 } }, "source": [ "# Initialisin i to 0\n", "i = 0 \n", "while i < 10:\n", " print(i) # 0–9\n", " i = i+ 1" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "4FCLLvUZrchJ", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "awbJtgTcrePT", "colab_type": "text" }, "source": [ "### 8) List methods." ] }, { "cell_type": "markdown", "metadata": { "id": "EILdnNLFrh8H", "colab_type": "text" }, "source": [ "Below are some of the most common list methods shown with an example." ] }, { "cell_type": "markdown", "metadata": { "id": "GaUe6W8Grj_-", "colab_type": "text" }, "source": [ "**a) List append method**" ] }, { "cell_type": "markdown", "metadata": { "id": "jwmNBqgZrlly", "colab_type": "text" }, "source": [ "The list.append( ) just appends an element to the list but never returns anything, it just appends the element to the existing list." ] }, { "cell_type": "code", "metadata": { "id": "Nw50C6swroMu", "colab_type": "code", "outputId": "3ac85806-4f4f-4cfa-d8c6-49f80a2cb8f1", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Creating a new list called name\n", "name = ['Tanu', 'Nanda']\n", "# Before appending\n", "name" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Tanu', 'Nanda']" ] }, "metadata": { "tags": [] }, "execution_count": 19 } ] }, { "cell_type": "code", "metadata": { "id": "iwX3dcwOrsfC", "colab_type": "code", "outputId": "dd6b3bd0-27ac-41a3-dfe2-765a136a8197", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Using the append operation on the current (\"Prabhu\") is the value to be appended.\n", "name.append(\"Prabhu\")\n", "# After appending\n", "name" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Tanu', 'Nanda', 'Prabhu']" ] }, "metadata": { "tags": [] }, "execution_count": 20 } ] }, { "cell_type": "markdown", "metadata": { "id": "bQfquS1srumK", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "iVTgbpCkrwpG", "colab_type": "text" }, "source": [ "**b) List insert method**" ] }, { "cell_type": "markdown", "metadata": { "id": "Rl6hyiG3ry71", "colab_type": "text" }, "source": [ "List.insert( ) operation is used to insert the elements inside a list with a specified index position." ] }, { "cell_type": "code", "metadata": { "id": "3lGudkynrwe0", "colab_type": "code", "outputId": "e73c5dbf-fe76-421f-9bb1-bb75b35aa6ee", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Creating a new list called list\n", "name = ['Tanu', 'Nanda']\n", "# Before inserting\n", "name" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Tanu', 'Nanda']" ] }, "metadata": { "tags": [] }, "execution_count": 21 } ] }, { "cell_type": "code", "metadata": { "id": "ee2uY9Ncr1q5", "colab_type": "code", "outputId": "132c1dc3-da78-48f8-c68f-617709410301", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Insert Operation on the existing list, here the index position is 2.\n", "name.insert(2, \"Prabhu\")\n", "# After Inserting\n", "name" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Tanu', 'Nanda', 'Prabhu']" ] }, "metadata": { "tags": [] }, "execution_count": 22 } ] }, { "cell_type": "markdown", "metadata": { "id": "pAfETynSr4uG", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "VVkPpvTor9NI", "colab_type": "text" }, "source": [ "**c) List extend method**" ] }, { "cell_type": "markdown", "metadata": { "id": "XhbWaUlur-U0", "colab_type": "text" }, "source": [ "The extend method adds the elements to the end of the new list. It is similar to append but you have to pass the list as an argument whereas in append it's not necessarily true." ] }, { "cell_type": "code", "metadata": { "id": "KOp3wcT0r_bA", "colab_type": "code", "outputId": "46e6f24f-28ca-4ecf-aa3b-cd6a6663789c", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Creating a new list called name\n", "name = ['Tanu', 'Nanda']\n", "# Before extending\n", "name" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Tanu', 'Nanda']" ] }, "metadata": { "tags": [] }, "execution_count": 23 } ] }, { "cell_type": "code", "metadata": { "id": "v8bWVXgKsCJ4", "colab_type": "code", "outputId": "b7cf1911-7f06-415a-c079-82fc2bc4ab56", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Using extend but make sure that you put the elements in a []\n", "# Without []\n", "name.extend(\"Prabhu\")\n", "# After extending\n", "name" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Tanu', 'Nanda', 'P', 'r', 'a', 'b', 'h', 'u']" ] }, "metadata": { "tags": [] }, "execution_count": 24 } ] }, { "cell_type": "code", "metadata": { "id": "oN64LF0MsD83", "colab_type": "code", "outputId": "25418d23-c9c7-4eff-f2f9-1a573a0187c6", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# After []\n", "name.extend([\"Prabhu\"])\n", "name" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Tanu', 'Nanda', 'P', 'r', 'a', 'b', 'h', 'u', 'Prabhu']" ] }, "metadata": { "tags": [] }, "execution_count": 25 } ] }, { "cell_type": "markdown", "metadata": { "id": "xqCuTkLusFt5", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "5jF1kglBsGev", "colab_type": "text" }, "source": [ "**d) List index method**" ] }, { "cell_type": "markdown", "metadata": { "id": "PccBohs7sGib", "colab_type": "text" }, "source": [ "The index operation in list searches the element in the list and then returns the index of that element. If the element is not present in the list then the index method returns a value error telling that the element is not in the list." ] }, { "cell_type": "code", "metadata": { "id": "4cXXfsd9sKTM", "colab_type": "code", "outputId": "918c0f84-1905-487c-9523-85edb93cccc9", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Creating a new list called name\n", "name = ['Tanu', 'Nanda']\n", "# Before indexing\n", "name" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Tanu', 'Nanda']" ] }, "metadata": { "tags": [] }, "execution_count": 26 } ] }, { "cell_type": "code", "metadata": { "id": "obyo2Tb_sNGE", "colab_type": "code", "outputId": "a593d0d7-2ee6-466d-d7bb-40836f0cf2b9", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# After indexing, type the element that you want to index.\n", "name.index(\"Nanda\")" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1" ] }, "metadata": { "tags": [] }, "execution_count": 27 } ] }, { "cell_type": "code", "metadata": { "id": "bV2dRJcpsO0M", "colab_type": "code", "outputId": "37edc679-0f6b-470e-f916-2caa08e0a72e", "colab": { "base_uri": "https://localhost:8080/", "height": 166 } }, "source": [ "# If the element is not present then you get an error\n", "name.index(\"Mercedes\")" ], "execution_count": 0, "outputs": [ { "output_type": "error", "ename": "ValueError", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\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[0mname\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Mercedes\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: 'Mercedes' is not in list" ] } ] }, { "cell_type": "markdown", "metadata": { "id": "C4hjJD5HsP-s", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Axe6nM7nsSW6", "colab_type": "text" }, "source": [ "**e) List remove method**" ] }, { "cell_type": "markdown", "metadata": { "id": "8TmYnmFUsTNz", "colab_type": "text" }, "source": [ "The Remove method in lists searches for the element and then on matching removes the element present in the list. Also throws an error when the element is not in the list." ] }, { "cell_type": "code", "metadata": { "id": "8mALjCzasUcw", "colab_type": "code", "outputId": "347cd5a0-1b30-426f-900e-fb8e6e48b028", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Creating a new list called name\n", "name = ['Tanu', 'Nanda']\n", "# Before removing\n", "name" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Tanu', 'Nanda']" ] }, "metadata": { "tags": [] }, "execution_count": 29 } ] }, { "cell_type": "code", "metadata": { "id": "QT9cFWTjsXUC", "colab_type": "code", "outputId": "c2a7d3b5-68bd-46a8-9008-f81ff39c4409", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# After removing\n", "name.remove(\"Nanda\")\n", "name" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Tanu']" ] }, "metadata": { "tags": [] }, "execution_count": 30 } ] }, { "cell_type": "code", "metadata": { "id": "SfUWaW4RsgXB", "colab_type": "code", "outputId": "d23772a6-e271-4a59-f840-9361f30836cc", "colab": { "base_uri": "https://localhost:8080/", "height": 166 } }, "source": [ "name.remove('Prabhu')" ], "execution_count": 0, "outputs": [ { "output_type": "error", "ename": "ValueError", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\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[0mname\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Prabhu'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: list.remove(x): x not in list" ] } ] }, { "cell_type": "markdown", "metadata": { "id": "_7fvqW5_siBu", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "sc5fdFfgslEm", "colab_type": "text" }, "source": [ "**f) List sort method**" ] }, { "cell_type": "markdown", "metadata": { "id": "Op96zt_zstcE", "colab_type": "text" }, "source": [ "As the name suggests the sort method sort the elements in ascending order. This method doesn't return anything." ] }, { "cell_type": "code", "metadata": { "id": "spkCBNK3su8F", "colab_type": "code", "outputId": "5a749fae-bec8-4767-f7ed-d7e44835a0d6", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Creating a list called num\n", "num = [1, 4, 5, 2, 3]\n", "# Before sorting\n", "num" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[1, 4, 5, 2, 3]" ] }, "metadata": { "tags": [] }, "execution_count": 32 } ] }, { "cell_type": "code", "metadata": { "id": "0BucUSzVsxVf", "colab_type": "code", "outputId": "576cfe00-fc04-4cad-90b6-9678f2ac764a", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# After sorting\n", "num.sort()\n", "num" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "metadata": { "tags": [] }, "execution_count": 33 } ] }, { "cell_type": "markdown", "metadata": { "id": "vD6evrQmsyxG", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "eukKn9FeszQg", "colab_type": "text" }, "source": [ "**g) List Reverse method**" ] }, { "cell_type": "markdown", "metadata": { "id": "WzEDCVhSs0E-", "colab_type": "text" }, "source": [ "As the name suggests the reverse method reverses the entire list. This method doesn't return anything." ] }, { "cell_type": "code", "metadata": { "id": "71BJzsYxs1g-", "colab_type": "code", "outputId": "1adc80c4-13c4-433d-e2fa-2b98d402693f", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Creating a list called num\n", "num = [1, 2, 3, 4, 5]\n", "# Before Reversing\n", "num" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "metadata": { "tags": [] }, "execution_count": 34 } ] }, { "cell_type": "code", "metadata": { "id": "t8nJU-kUs4HF", "colab_type": "code", "outputId": "7bddbf25-949b-4797-b3da-f1a54c21cb3a", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "num.reverse()\n", "# After the reverse\n", "num" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[5, 4, 3, 2, 1]" ] }, "metadata": { "tags": [] }, "execution_count": 35 } ] }, { "cell_type": "markdown", "metadata": { "id": "7QyNm5ivs54W", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "THOAJzVHs6vX", "colab_type": "text" }, "source": [ "**h) List pop method**" ] }, { "cell_type": "markdown", "metadata": { "id": "EiTH117ls8WQ", "colab_type": "text" }, "source": [ "The Pop method removes and returns the element at the given index. Returns the rightmost element if the index is omitted (roughly the opposite of append( ))." ] }, { "cell_type": "code", "metadata": { "id": "Vf3_3htss9fq", "colab_type": "code", "outputId": "de615997-224c-42c2-ea72-b707a79d76eb", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Creating a list called num\n", "num = [1, 2, 3, 4, 5]\n", "# Before popping\n", "num" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "metadata": { "tags": [] }, "execution_count": 36 } ] }, { "cell_type": "code", "metadata": { "id": "bMCUHjeDtAAO", "colab_type": "code", "outputId": "2b97eac9-e47b-4668-9776-db70029a52d5", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# After popping\n", "num.pop(1)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "2" ] }, "metadata": { "tags": [] }, "execution_count": 37 } ] }, { "cell_type": "code", "metadata": { "id": "wSuKIqIJtBgz", "colab_type": "code", "outputId": "d0d98514-5079-47f7-96d6-2d25100e85f7", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "num" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[1, 3, 4, 5]" ] }, "metadata": { "tags": [] }, "execution_count": 38 } ] }, { "cell_type": "markdown", "metadata": { "id": "KTdw3P1itC40", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "EFdwcMl7tDov", "colab_type": "text" }, "source": [ "### 9) List Slices" ] }, { "cell_type": "markdown", "metadata": { "id": "UN9w53d4tFIK", "colab_type": "text" }, "source": [ "Slicing the list means cutting down the list elements, it and can also be used to change sub-parts of the list." ] }, { "cell_type": "code", "metadata": { "id": "T7oLm3rNtGRe", "colab_type": "code", "outputId": "ac0d9e91-5c0f-478a-f4ff-7eae02a7a10a", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "char = ['a', 'b', 'c', 'd']\n", "char\n", "['a', 'b', 'c', 'd']\n", "char[1:-1] ## ['b', 'c']\n", "char" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['a', 'b', 'c', 'd']" ] }, "metadata": { "tags": [] }, "execution_count": 39 } ] }, { "cell_type": "code", "metadata": { "id": "R1SAANJsvzyQ", "colab_type": "code", "outputId": "c81665bf-0ed4-4d71-f823-cf4ceb230698", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "char[0:2] = 'z' ## replace ['a', 'b'] with ['z']\n", "char ## ['z', 'c', 'd']" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['z', 'c', 'd']" ] }, "metadata": { "tags": [] }, "execution_count": 40 } ] }, { "cell_type": "markdown", "metadata": { "id": "XutVXpNRtKhr", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "D4A_Ydl1tMBW", "colab_type": "text" }, "source": [ "### 10) Converting a string to a list" ] }, { "cell_type": "markdown", "metadata": { "id": "4uMwYVRktNDy", "colab_type": "text" }, "source": [ "This is the most important technique, often we need to convert the string to a list in our projects, this is an easy approach that does the job for us. This below example was referred from [geeksforgeeks.com](https://www.geeksforgeeks.org/python-program-convert-string-list/). Here we are using the split method from the strings where the split method just splits the elements from the string and then broadcasts into a list as shown below." ] }, { "cell_type": "code", "metadata": { "id": "rGlOVSV3taFK", "colab_type": "code", "outputId": "a120c7ef-daab-49f2-a299-fef120d604f4", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Creating a String\n", "String = \"Tanu\"\n", "String" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Tanu'" ] }, "metadata": { "tags": [] }, "execution_count": 1 } ] }, { "cell_type": "code", "metadata": { "id": "vzTQPbGNtbwN", "colab_type": "code", "outputId": "b125c392-1079-4c5d-eb1b-190cc2bca28a", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Checking for the type \n", "type(String)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "str" ] }, "metadata": { "tags": [] }, "execution_count": 2 } ] }, { "cell_type": "code", "metadata": { "id": "BjB0Wel4tdUo", "colab_type": "code", "outputId": "11c76408-f09d-43ef-a0d2-380ad0eea0e1", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Using the split method to split the string into a list.\n", "name = list(String.split(\" \")) \n", "name" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Tanu']" ] }, "metadata": { "tags": [] }, "execution_count": 3 } ] }, { "cell_type": "code", "metadata": { "id": "iL81efS3wwdh", "colab_type": "code", "outputId": "ece77344-d8c5-4dc5-ad79-3f8cfc6b3d76", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "type(name)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "list" ] }, "metadata": { "tags": [] }, "execution_count": 5 } ] }, { "cell_type": "markdown", "metadata": { "id": "t223rcQi4AKR", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "KCYEjqg54KAN", "colab_type": "text" }, "source": [ "Hence above are the very important techniques or methods of Lists in Python. Most of the examples were referred from [Python Lists](https://developers.google.com/edu/python/lists) from Google. I have written this in a simple way such that everybody can understand and master the concepts of lists in Python. If you guys have some doubts in the code, the comment section is all yours." ] }, { "cell_type": "markdown", "metadata": { "id": "HAjHMPzy4M8P", "colab_type": "text" }, "source": [ "## ThankYou." ] } ] }