{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "da1a7116",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['h', 'u', 'm', 'a', 'n']\n"
     ]
    }
   ],
   "source": [
    "# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n",
    "# Python List Comprehension\n",
    "#\n",
    "#      https://www.programiz.com/python-programming/list-comprehension\n",
    "# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n",
    "\n",
    "# Program to separate the letters of the word \"human\" and \n",
    "# add the letters as items of a list:\n",
    "\n",
    "h_letters = []           # Create an EMPTY list\n",
    "\n",
    "for letter in 'human':\n",
    "    h_letters.append(letter)\n",
    "\n",
    "print(h_letters)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0b8349a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['h', 'u', 'm', 'a', 'n']\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "A list comprehension creates a new list by **applying** an operation\n",
    "to each element of a sequence.\n",
    "\n",
    "The general syntax is:\n",
    "\n",
    "       [ <expression with var_name> for <variable_name> in <sequence> ].\n",
    "\"\"\"\n",
    "\n",
    "# List comprehension is an elegant way to define and create lists based on existing lists.\n",
    "\n",
    "# Syntax of List Comprehension:\n",
    "#\n",
    "#       [operation(expr)-on-item for item in \"list\"]\n",
    "#\n",
    "# NOTE: this syntax works for all collectors and for strings\n",
    "#\n",
    "# Returns:\n",
    "#\n",
    "#       [expr(item0), expr(item1), .... ]   (item0, item1, ... are item in \"list\")\n",
    "#\n",
    "\n",
    "# Example:\n",
    "\n",
    "h_letters = [ letter for letter in 'human' ]\n",
    "print( h_letters)\n",
    "print(type(h_letters))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1f582bb6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# Example 2:\n",
    "#\n",
    "# Create a list of squares from a list of values\n",
    "\n",
    "o = [ x*x for x in (1,2,3) ]\n",
    "print(o)\n",
    "print(type(o))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "daaa495f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<generator object <genexpr> at 0x7f9cd8750660>\n",
      "1\n",
      "4\n",
      "9\n",
      "<class 'generator'>\n"
     ]
    }
   ],
   "source": [
    "# Example 3:\n",
    "#\n",
    "# Create a tuple of squares from a *tuple* of values\n",
    "\n",
    "o = (x*x for x in (1,2,3))\n",
    "print(o)\n",
    "for i in o:\n",
    "    print(i)\n",
    "print(type(o))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a74098f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 4, 9}\n",
      "<class 'set'>\n"
     ]
    }
   ],
   "source": [
    "# Example 4:\n",
    "#\n",
    "# Create a set of squares from a *set* of values\n",
    "\n",
    "o = { x*x for x in (1,2,3) }\n",
    "print(o)\n",
    "print(type(o))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "04e61b67",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "************************************************************\n",
    "Conditionals in list comprehension\n",
    "************************************************************\n",
    "\n",
    "General Syntax\n",
    "\n",
    "       [ <expression> for <variable_name> in <sequence> if <condition>]\n",
    "\n",
    "What it means:\n",
    "\n",
    "       result = []\n",
    "       for variable_name in sequence:\n",
    "       if condition:\n",
    "           result.append(expression)\n",
    "           \n",
    "History:\n",
    "\n",
    "   List comprehensions come from math (set-builder notation).\n",
    "\n",
    "        a = [ x * x for x in s if x > 0 ] # Python\n",
    "\n",
    "        a = { x^2 | x ∈ s, x > 0 }         # Math\n",
    "\"\"\"\n",
    "\n",
    "number_list = [ x for x in range(20) if x % 2 == 0]\n",
    "#                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ x = 0,1,2,3,...19 and x%2==0\n",
    "print(number_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "4cfd8b05",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]\n"
     ]
    }
   ],
   "source": [
    "num_list = [x for x in range(100) if x % 2 == 0 if x % 5 == 0]\n",
    "#                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ x = 0,1,2,3,...99 and x%2==0 and x%5==0\n",
    "print(num_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "4f7aad93",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, -1, 2, -3, 4]\n"
     ]
    }
   ],
   "source": [
    "num_list = [x if (x % 2 == 0) else -x for x in range(5)]\n",
    "print(num_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "9869bb44",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, -3, 6, -9, 12, -15, 18]\n"
     ]
    }
   ],
   "source": [
    "num_list = [x if (x%2==0) else -x for x in range(20) if x%3==0]\n",
    "print(num_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "555e6d40",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2, 3, 4], [4, 5, 6, 8]]\n",
      "[1, 2, 3, 4]\n",
      "1\n",
      "4\n",
      "[1, 4]\n",
      "2\n",
      "5\n",
      "[2, 5]\n",
      "3\n",
      "6\n",
      "[3, 6]\n",
      "4\n",
      "8\n",
      "[4, 8]\n",
      "[[1, 4], [2, 5], [3, 6], [4, 8]]\n"
     ]
    }
   ],
   "source": [
    "# *********************************************************\n",
    "# Nested loops in list comprehension\n",
    "# *********************************************************\n",
    "\n",
    "# Program to transpose a matrix:\n",
    "\n",
    "\n",
    "matrix = [[1, 2, 3, 4], [4, 5, 6, 8]]\n",
    "print(matrix)\n",
    "\n",
    "transposed = []\n",
    "\n",
    "print(matrix[0])\n",
    "\n",
    "for j in range(len(matrix[0])):        # for j = 0,1,2,3  (column j of matrix)\n",
    "    transposed_row = []\n",
    "\n",
    "    for row in matrix:                 # row goes through all rows in matrix\n",
    "        print(row[j])                  # Picks out elem in column j\n",
    "        transposed_row.append(row[j])\n",
    "    print(transposed_row)\n",
    "    transposed.append(transposed_row)\n",
    "\n",
    "print(transposed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8720b6dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "[1, 3, 5, 7]\n",
      "[2, 4, 6, 8]\n",
      "[[1, 3, 5, 7], [2, 4, 6, 8]]\n",
      "2\n",
      "[[1, 3, 5, 7], [2, 4, 6, 8]]\n"
     ]
    }
   ],
   "source": [
    "# You can even use list comprehension to invert a matrix:\n",
    "\n",
    "\n",
    "matrix = [[1, 2], [3,4], [5,6], [7,8]]\n",
    "\n",
    "print(len(matrix[0]))\n",
    "\n",
    "# Make first row:\n",
    "#    pick elem[0] from each row in matrix\n",
    "print([row[0] for row in matrix])\n",
    "\n",
    "# Make 2nd row:\n",
    "#    pick elem[1] from each row in matrix\n",
    "print([row[1] for row in matrix])\n",
    "\n",
    "# How to transpose THIS matrix:\n",
    "transpose = [[row[i] for row in matrix] for i in range(2)]\n",
    "print (transpose)\n",
    "\n",
    "# How to transpose a matrix in general:\n",
    "print(len(matrix[0]))\n",
    "transpose = [[row[i] for row in matrix] for i in range(len(matrix[0]))]\n",
    "print (transpose)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "94747185",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 'a'), (2, 'b'), (3, 'c')]\n",
      "{(1, 'a'), (2, 'b'), (3, 'c')}\n",
      "{1: 'a', 2: 'b', 3: 'c'}\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "There are 3 different types of comprehensions:\n",
    "\n",
    "   List, set and dictionary\n",
    "\"\"\"\n",
    "\n",
    "# List comprehension:   [ expr  for ...  ]     (result is a list)\n",
    "L = [ ('a',1), ('b',2), ('c',3) ]\n",
    "\n",
    "print( [ (x[1], x[0]) for x in L ] )\n",
    "\n",
    "# Set comprehension:   { expr  for ...  }     (result is a set)\n",
    "L = [ ('a',1), ('b',2), ('c',3) ] \n",
    "\n",
    "print( { (x[1], x[0]) for x in L } )\n",
    "\n",
    "# Dictionary comprehension:   { key:val  for ...  }     (result is a dictionary)\n",
    "L = [ ('a',1), ('b',2), ('c',3) ] \n",
    "\n",
    "print( { x[1]:x[0] for x in L } )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1753063c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
