{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists \n", "* List items are ordered, changeable, and allow duplicate values.\n", "* List items are indexed, the first item has index [0], the second item has index [1] etc \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Creating a List**" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Apple', 'Banana', 'Orange', 'Data', 'Test']\n" ] } ], "source": [ "myList = ['Apple', 'Banana', 'Orange', 'Data', 'Test']\n", "\n", "print(myList)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Apple\n", "Banana\n", "Orange\n", "Data\n", "Test\n" ] } ], "source": [ "# printing each element\n", "for i in myList:\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Another way to Create a list**" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n" ] } ], "source": [ "myList1 = list() # Empty List\n", "print(myList1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Adding Elements**" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['One', 'Two']\n" ] } ], "source": [ "myList1.append('One')\n", "myList1.append('Two')\n", "print(myList1)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2.2, 'One', 3, 'Integer']\n" ] } ], "source": [ "# Creating an List by direct assignment\n", "myList2 = list([2.2, 'One', 3, 'Integer'])\n", "print(myList2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Selecting an element by index value**" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.2\n" ] } ], "source": [ "print(myList2[0])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "print(myList2[2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Out of Index Error** \n", "List index starts from 0 till n-1 where n is the no of element in the list" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmyList2\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "print(myList2[4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Slicing** \n", "Way to select or slice element from list" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Orange', 'Data', 'Test']\n" ] } ], "source": [ "# Select all element from index 2 to end\n", "print(myList[2:]) " ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Test\n" ] } ], "source": [ "# Get last element \n", "# Index nos will start from -1 to -n in backward list where n is the no of elements in list\n", "print(myList[-1])" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Orange', 'Data']\n" ] } ], "source": [ "# Get element from index 2 except the last one\n", "print(myList[2:-1])" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Test', 'Data', 'Orange', 'Banana', 'Apple']\n" ] } ], "source": [ "# Reversing the whole list\n", "print(myList[::-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Element Exists??**" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[True]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "['Test' in myList]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[False]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "['Mango' in myList]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Banana' in myList" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**No of elements in list or lenght**" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(myList)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Manupulating a List**" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Apple', 'Banana', 'Orange', 'Data', 'Test', 0]\n" ] } ], "source": [ "# Appending an element 0\n", "myList.append(0)\n", "print(myList)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Apple', '2', 'Banana', 'Orange', 'Data', 'Test', 0]\n" ] } ], "source": [ "# Inserting an element at index 1 (2nd element)\n", "myList.insert(1, '2')\n", "print(myList)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "['Apple', '2', 'Banana', 'Orange', 'Data', 'Test']\n" ] } ], "source": [ "# Removing the last element\n", "last = myList.pop()\n", "print(last)\n", "print(myList)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n", "['Apple', 'Banana', 'Orange', 'Data', 'Test']\n" ] } ], "source": [ "# Removing the element 2\n", "item = myList.remove('2')\n", "print(item)\n", "print(myList)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "list.remove(x): x not in list", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Removing element which does not exists\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mitem\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmyList\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Pop'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mitem\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmyList\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list" ] } ], "source": [ "# Removing element which does not exists\n", "item = myList.remove('Pop')\n", "print(item)\n", "print(myList)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n" ] } ], "source": [ "# Clearing the whole list \n", "myList2.clear() # can be done as re-initialization the variable --> myLList2 =[]\n", "print(myList2)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Test', 'Data', 'Orange', 'Banana', 'Apple']\n" ] } ], "source": [ "myList.reverse() # inplace reverse\n", "print(myList)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Apple', 'Banana', 'Data', 'Orange', 'Test']\n" ] } ], "source": [ "myList.sort() # inplace sort\n", "print(myList)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "list2 before --> ['Apple', 'Banana', 'Data', 'Orange', 'Test']\n", "list2 after --> ['Test', 'Orange', 'Data', 'Banana', 'Apple']\n", "myList --> ['Apple', 'Banana', 'Data', 'Orange', 'Test']\n" ] } ], "source": [ "list2 = myList.copy() # creating a copy\n", "print(\"list2 before --> \",list2)\n", "list2.reverse()\n", "print(\"list2 after --> \",list2)\n", "print(\"myList --> \",myList)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "list2 --> ['Test', 'Orange', 'Data', 'Banana', 'Apple']\n", "list3 --> ['Apple', 'Banana', 'Data', 'Orange', 'Test']\n" ] } ], "source": [ "list3 = sorted(list2) # Create another copy\n", "print(\"list2 --> \",list2)\n", "print(\"list3 --> \",list3)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "# clearing all the list\n", "myList=[]\n", "myList2=[]\n", "list2=[]\n", "list3=[]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Concat**" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "list2 = ['this', 'is', 'another', 'data']\n", "list3 = ['which', 'we', 'are', 'going', 'to', 'manipulate']" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "list2 --> ['this', 'is', 'another', 'data']\n", "list3 --> ['which', 'we', 'are', 'going', 'to', 'manipulate']\n", "['this', 'is', 'another', 'data', 'which', 'we', 'are', 'going', 'to', 'manipulate']\n" ] } ], "source": [ "print(\"list2 --> \", list2)\n", "print(\"list3 --> \", list3)\n", "\n", "myList = list2 + list3\n", "print(myList)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['this', 'is', 'another', 'data', 'which', 'we', 'are', 'going', 'to', 'manipulate']\n" ] } ], "source": [ "# Print all elements\n", "print(myList[:])" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['which', 'we', 'are']\n" ] } ], "source": [ "# Get element from index 4 till 6\n", "print(myList[4:7])" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['this', 'is']\n" ] } ], "source": [ "# Get element from 0 to 1\n", "print(myList[:2])" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['this', 'another', 'which', 'are']\n" ] } ], "source": [ "# Get element from 0 to 6 with 2 hops/steps\n", "print(myList[:7:2])" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['this', 'which', 'to']\n" ] } ], "source": [ "# Get all element with 4 hops\n", "print(myList[::4])" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['manipulate', 'we', 'is']\n" ] } ], "source": [ "# Get all element with 4 hops but backward\n", "print(myList[::-4])" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['manipulate', 'to', 'going', 'are', 'we', 'which', 'data', 'another', 'is', 'this']\n" ] } ], "source": [ "# Reverse the whole list\n", "print(myList[::-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Copy List**" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "list2 --> ['this', 'is', 'another', 'data']\n", "list4 --> ['this', 'is', 'another', 'data']\n", "list5 --> ['this', 'is', 'another', 'data']\n", "list6 --> ['this', 'is', 'another', 'data']\n" ] } ], "source": [ "list4 = list2 # Copy be reference\n", "list5 = list2.copy() \n", "list6 = list2[:]\n", "\n", "print(\"list2 --> \", list2)\n", "print(\"list4 --> \", list4)\n", "print(\"list5 --> \", list5)\n", "print(\"list6 --> \", list6)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "list2 --> ['this', 'are', 'another', 'data']\n", "list4 --> ['this', 'are', 'another', 'data']\n", "list5 --> ['this', 'is', 'another', 'data']\n", "list6 --> ['this', 'is', 'another', 'data']\n" ] } ], "source": [ "list2[1] = 'are' # altering list2 2nd element\n", "\n", "print(\"list2 --> \", list2)\n", "print(\"list4 --> \", list4)\n", "print(\"list5 --> \", list5)\n", "print(\"list6 --> \", list6)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "list2 --> ['this', 'are', 'another', 'data', 'extra', 'extra']\n", "list4 --> ['this', 'are', 'another', 'data', 'extra', 'extra']\n", "list5 --> ['this', 'is', 'another', 'data']\n", "list6 --> ['this', 'is', 'another', 'data']\n" ] } ], "source": [ "list2.append('extra')\n", "\n", "print(\"list2 --> \", list2)\n", "print(\"list4 --> \", list4)\n", "print(\"list5 --> \", list5)\n", "print(\"list6 --> \", list6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**List Comprehension**" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n" ] } ], "source": [ "# List Comprehension\n", "\n", "arr = list(range(1,20))\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]\n" ] } ], "source": [ "# Creating another list from arr\n", "arr2 = [ i*i for i in arr]\n", "print(arr2)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 4, 6, 8, 10, 12, 14, 16, 18]" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Fetch only even value via comprehention\n", "[i for i in arr if i%2==0 ]" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 2, 0, 4, 0, 6, 0, 8, 0, 10, 0, 12, 0, 14, 0, 16, 0, 18, 0]" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[i if i%2==0 else 0 for i in arr ]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.8.5" } }, "nbformat": 4, "nbformat_minor": 5 }