{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# map()\n", "\n", "map() is a built-in Python function that takes in two or more arguments: a function and one or more iterables, in the form:\n", "\n", " map(function, iterable, ...)\n", " \n", "map() returns an *iterator* - that is, map() returns a special object that yields one result at a time as needed. We will learn more about iterators and generators in a future lecture. For now, since our examples are so small, we will cast map() as a list to see the results immediately.\n", "\n", "When we went over list comprehensions we created a small expression to convert Celsius to Fahrenheit. Let's do the same here but use map:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def fahrenheit(celsius):\n", " return (9/5)*celsius + 32\n", " \n", "temps = [0, 22.5, 40, 100]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's see map() in action:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[32.0, 72.5, 104.0, 212.0]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "F_temps = map(fahrenheit, temps)\n", "\n", "#Show\n", "list(F_temps)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the example above, map() applies the fahrenheit function to every item in temps. However, we don't have to define our functions beforehand; we can use a lambda expression instead:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[32.0, 72.5, 104.0, 212.0]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(map(lambda x: (9/5)*x + 32, temps))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Great! We got the same result! Using map with lambda expressions is much more common since the entire purpose of map() is to save effort on having to create manual for loops." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### map() with multiple iterables\n", "map() can accept more than one iterable. The iterables should be the same length - in the event that they are not, map() will stop as soon as the shortest iterable is exhausted.\n", "\n", "\n", "For instance, if our function is trying to add two values **x** and **y**, we can pass a list of **x** values and another list of **y** values to map(). The function (or lambda) will be fed the 0th index from each list, and then the 1st index, and so on until the n-th index is reached.\n", "\n", "Let's see this in action with two and then three lists:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[6, 8, 10, 12]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1,2,3,4]\n", "b = [5,6,7,8]\n", "c = [9,10,11,12]\n", "\n", "list(map(lambda x,y:x+y,a,b))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[15, 18, 21, 24]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Now all three lists\n", "list(map(lambda x,y,z:x+y+z,a,b,c))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can see in the example above that the parameter **x** gets its values from the list **a**, while **y** gets its values from **b** and **z** from list **c**. Go ahead and play with your own example to make sure you fully understand mapping to more than one iterable.\n", "\n", "Great job! You should now have a basic understanding of the map() function." ] } ], "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.2" } }, "nbformat": 4, "nbformat_minor": 1 }