{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "148f7417",
   "metadata": {},
   "source": [
    "# Filter()\n",
    "* The filter() function construct a list from those elements of the list for which a function returns True.\n",
    "#### filter(func, iterable)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "51f9d9bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[15, 20, 35, 12, 56, 89, 34, 23, 67]\n"
     ]
    }
   ],
   "source": [
    "num=[15,20,35,2,6,12,56,89,34,23,67,10]\n",
    "\n",
    "def greater_10(n):\n",
    "    if n>10:\n",
    "        return 1\n",
    "    \n",
    "if __name__=='__main__':\n",
    "    rslt = list(filter(greater_10,num))\n",
    "    print(rslt)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06d84bba",
   "metadata": {},
   "source": [
    "* The filter() function return True or False.\n",
    "* Only values which are greater than 10 are visible in the newly created list."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf4392eb",
   "metadata": {},
   "source": [
    "## map()\n",
    "* The map function applies a particular funtion to every element of a list.\n",
    "* map(func,iterable)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "b09a0158",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7.5, 10.0, 17.5, 1.0, 3.0, 6.0, 28.0, 44.5, 17.0, 11.5, 33.5, 5.0]\n"
     ]
    }
   ],
   "source": [
    "num1=[15,20,35,2,6,12,56,89,34,23,67,10]\n",
    "\n",
    "def divide_2(n):\n",
    "    return n/2\n",
    "\n",
    "\n",
    "\n",
    "if __name__=='__main__':\n",
    "    new_list = list(map(divide_2,num1))\n",
    "    print(new_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e262462",
   "metadata": {},
   "source": [
    "* After applying a function on the list the map() returns a modified list.\n",
    "* You can pass any no. of iterable to map function.\n",
    "* fucntion should have same no. of arguments as the no. of iterables in map function.\n",
    "* Each argument is called with corresponding item for each sequence."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "0155fae3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[89, 80, 94, 50, 67, 79]\n"
     ]
    }
   ],
   "source": [
    "a_marks=[75,80,68,45,67,58]\n",
    "b_marks=[89,67,94,50,49,79]\n",
    "\n",
    "def compare(n,n2):\n",
    "    if n>n2:\n",
    "        return n\n",
    "    else:\n",
    "        return n2\n",
    "    \n",
    "if __name__=='__main__':\n",
    "    max_marks = list(map(compare,a_marks,b_marks))\n",
    "    print(max_marks)\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "642e284b",
   "metadata": {},
   "source": [
    "### Reduce()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5d6bae9",
   "metadata": {},
   "source": [
    "* The reduce() fucntion with syntax as given below returns a single value generated by calling the function on the first two items of the iterable, then on the result and the next item.\n",
    "* reduce(func,iterable)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "23b434ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "380\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "\n",
    "marks=[33,56,78,90,56,67]\n",
    "\n",
    "def add(a,b):\n",
    "    return a+b\n",
    "\n",
    "print(reduce(add,marks))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74eaca28",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea0e0985",
   "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
