{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 005_lists\n", "\n", "[Source](https://github.com/iArunava/Python-TheNoTheoryGuide/)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Simple Lists\n", "names = [\"Jennifer\", \"Python\", \"Scarlett\"]\n", "nums = [1, 2, 3, 4, 5]\n", "chars = ['A', 'q', 'E', 'z', 'Y']\n", "\n", "print (names)\n", "print (nums)\n", "print (chars)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Can have multiple data types in one list\n", "rand_list = [\"Jennifer\", \"Python\", \"refinneJ\", 'J', '9', 9, 12.90, \"Who\"]\n", "print (rand_list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Accessing elements in a list\n", "# O-indexed\n", "print (names[2])\n", "print (rand_list[3])\n", "print (names[0] + \" \" + rand_list[2].title())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Negetive indexes: Access elements from the end of the list without knowing the size of the list\n", "print (rand_list[-1]) # Returns the last element of the list [1st from the end]\n", "print (rand_list[-2]) # Returns the 2nd last element\n", "# and so on.." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Now here's a question.\n", "print (rand_list[-1] + \" is \" + names[2] + \"?\")\n", "print (\"A) \" + rand_list[0] + \"'s sister\\tB) \" + names[0] + \"'s Friend\\nC) Not Related to \" + rand_list[-8] + \"\\tD) Nice question but I don't know\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Modifying elements in a list\n", "str_list = [\"Scarlett\", \"is\", \"a\", \"nice\", 'girl', '!']\n", "\n", "print (str_list)\n", "str_list[0] = \"Jennifer\"\n", "print (str_list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Adding elements to a list\n", "# Use append() to add elements to the end of the list\n", "str_list.append ('She is 21.')\n", "print (str_list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# So, you can build lists like this\n", "my_list = []\n", "my_list.append (\"myname\")\n", "my_list.append (\"myage\")\n", "my_list.append (\"myaddress\")\n", "my_list.append (\"myphn\")\n", "my_list.append (\"is\")\n", "my_list.append (1234567890)\n", "print (my_list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Insert elements at specific positions of the list\n", "# insert(index, element)\n", "my_list.insert (0, \"Mr/Miss/Mrs\")\n", "print (my_list)\n", "\n", "my_list.insert(4, \"mybday\")\n", "print (my_list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Using '-1' to insert at the end doesn't work and inserts element at the 2nd last position.\n", "my_list = ['A', 'B', 'C', 'D']\n", "my_list.insert (-1, 'E')\n", "print (my_list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Using '-2' inserts at 3rd last position\n", "# In general, use '-n' to insert at 'n+1'th position from end.\n", "my_list = ['A', 'B', 'C', 'D']\n", "my_list.insert (-2, 'E')\n", "print (my_list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Insert elements at the end\n", "l1 = ['A', 'B', 'C', 'D']\n", "l2 = ['A', 'B', 'C', 'D']\n", "\n", "l1.append('E')\n", "l2.insert(len(my_list), 'E')\n", "print (l1)\n", "print (l2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Length of the list\n", "l1 = ['A', 'B', 'C', 'D', 'E']\n", "print (len(l1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# # Removing elements from list\n", "# del can remove any element from list as long as you know its index\n", "l1 = ['A', 'B', 'C', 'D', 'E']\n", "print (l1)\n", "\n", "del l1[0]\n", "print (l1)\n", "\n", "del l1[-1]\n", "print (l1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# pop() can remove the last element from list when used without any arguments\n", "l1 = ['A', 'B', 'C', 'D', 'E']\n", "# pop() returns the last element, so c would store the popped element\n", "c = l1.pop()\n", "\n", "print (l1)\n", "print (c) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# pop(n) -> Removes the element at index 'n' and returns it\n", "l1 = ['A', 'B', 'C', 'D', 'E']\n", "\n", "# Removes the element at 0 position and returns it\n", "c = l1.pop(0)\n", "print (l1)\n", "print (c)\n", "\n", "# Works as expected with negetive indexes\n", "c = l1.pop(-1)\n", "print (l1)\n", "print (c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Removing an item by value\n", "# remove() only removes the first occurence of the value that is specified.\n", "q1 = [\"Seriously, \", \"what\", \"happened\", \"to\", \"Jennifer\", \"and\", \"Jennifer\", \"?\"]\n", "print (q1)\n", "\n", "q1.remove (\"Jennifer\")\n", "print (q1)\n", "\n", "n1 = \"and\"\n", "q1.remove(n1)\n", "print (q1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Sorting a list\n", "# sort() -> sorts list in increasing or decreasing order, *permantantly*\n", "# Sorts in alphabetical order\n", "l1 = ['E', 'D', 'C', 'B', 'A']\n", "l1.sort()\n", "print (l1)\n", "\n", "# Sorts in increasing order\n", "l2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", "l2.sort()\n", "print (l2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Reverse sorts alphabetical order\n", "l1 = ['E', 'D', 'C', 'B', 'A']\n", "l1.sort(reverse=True)\n", "print (l1)\n", "\n", "# Sorts in decreasing order\n", "l2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", "l2.sort(reverse=True)\n", "print (l2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# sorted() -> Sorts list in increasing or decreasing order, *temporarily*\n", "# Sorts in increasing order\n", "l2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", "print (l2)\n", "print (sorted(l2))\n", "print (l2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Sorts in decreasing order\n", "l2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", "print (l2)\n", "print (sorted(l2, reverse=True))\n", "print (l2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Reverse list\n", "l1 = ['E', 'D', 'C', 'B', 'A']\n", "l1.reverse()\n", "print (l1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Looping Through a list using for\n", "l1 = [\"Scarlett\", \"is\", \"now\", \"back\", \"from\", \"her first\", \"Python\", \"lesson.\"]\n", "\n", "# Do notice the indentations\n", "for each_word in l1:\n", " print (each_word)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Looping through a list using while\n", "l1 = [\"Scarlett\", \"is\", \"in\", \"love\", \"with\", \"Python\"]\n", "i = 0\n", "while i is not len(l1):\n", " print (l1[i])\n", " i += 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Numerical lists\n", "# Note: range(n, m) will loop over numbers from n to m-1\n", "l1 = ['A', 'B', 'C', 'D', 'E']\n", "print (\"Guess how much Scarlett scored in her first lesson out of 5:\")\n", "for val in range(1, 6):\n", " print (l1[val-1] + \") \" + str(val))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Using range() to make a list of numbers\n", "num_list = list(range(1, 6))\n", "print (num_list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Use range() to skip values at intervals\n", "# range (num_to_start_from, num_to_end_at+1, interval)\n", "l1 = list(range(10, 51, 5))\n", "print (l1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Operations with list of numbers with -> min() max() sum()\n", "l1 = [2, 3, 4, 45, 1, 5, 6, 3, 1, 23, 14]\n", "\n", "print (\"Sum: \" + str(sum(l1)))\n", "print (\"Max: \" + str(max(l1)))\n", "print (\"Min: \" + str(min(l1)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# List Comprehensions\n", "# Simple\n", "l1 = [i for i in range(20, 30, 1)]\n", "l2 = [i+1 for i in range(20, 30, 1)]\n", "l3 = [[i, i**2] for i in range(2, 12, 3)]\n", "print (l1)\n", "print (l2)\n", "print (l3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# A few more list comprehension examples\n", "equi_list_1 = [[x, y, z] for x in range(1, 3) for y in range(3, 6) for z in range(6, 9)]\n", "print (equi_list_1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# The above list comprehension is equivalent of the following code\n", "equi_list_2 = []\n", "for x in range(1, 3):\n", " for y in range(3, 6):\n", " for z in range(6, 9):\n", " equi_list_2.append([x, y, z])\n", "print (equi_list_2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Proof of equivalence (Do execute the above two blocks of code before running this)\n", "print (equi_list_1 == equi_list_2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# List Comprehension with conditionals\n", "l1 = [x if x%5==0 else \"blank\" for x in range(20)]\n", "print (l1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# One more list comprehension with conditionals\n", "l1 = [\"Jennifer\", \"met\", \"Scarlett\", \"in\", \"Python\", \"lessons\", \"they\", \"take.\"]\n", "l2 = [[str(x) + \") \" + y] for x in range(len(l1)) for y in l1 if l1[x] == y]\n", "print (l2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Slicing a list\n", "l1 = [\"Jennifer\", \"is\", \"now\", \"friends\", \"with\", \"Scarlett\"]\n", "\n", "# [start_index : end_index+1]\n", "print(\"[2:5] --> \" + str(l1[2:5]))\n", "print(\"[:4] --> \" + str(l1[:4])) # everthing before 4th index [excluding the 4th]\n", "print(\"[2:] --> \" + str(l1[2:])) # everything from 2nd index [including the 2nd]\n", "print(\"[:] --> \" + str(l1[:])) # every element in the list" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Some more slicing\n", "l1 = [\"Jennifer\", \"and\", \"Scarlett\", \"now\", \"Pythonistas\", \"!\"]\n", "\n", "print (\"[-2:] --> \" + str(l1[-2:]))\n", "print (\"[:-3] --> \" + str(l1[:-3]))\n", "print (\"[-5:-2] --> \" + str(l1[-5:-2]))\n", "print (\"[-4:-6] --> \" + str(l1[-4:-6]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Looping through a slice\n", "l1 = [\"Pythonistas\", \"rock\", \"!!!\", \"XD\"]\n", "for w in l1[-4:-1]:\n", " print (w.upper())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Copying a list\n", "l1 = [\"We\", \"should\", \"use\", \"[:]\", \"to\", \"copy\", \"the\", \"whole\", \"list\"]\n", "l2 = l1[:]\n", "print(l2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Proof that the above two lists are different\n", "l2.append(\". Using [:] ensures the two lists are different\")\n", "\n", "print (l1)\n", "print (l2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# What happens if we directly assign one list to the other instead of using slices\n", "l1 = [\"Jennifer\", \"now\", \"wonders\", \"what\", \"happens\", \"if\", \"we\", \"directly\", \"assign.\"]\n", "l2 = l1\n", "l2.append(\"Both variables point to the same list\")\n", "\n", "print (l1)\n", "print (l2)" ] } ], "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 }