Coder Social home page Coder Social logo

map-readme-python-al's Introduction

Map in Python

Objectives

  • Understand how map works in python for mapping a given function to each element of an iterable.
  • Differentiate between map and filter functions and identify suitable uses cases for each

Introduction

In the last section, we saw how the filter function selects a subset of a list by iterating through all elements and selecting only those that match a certain criterion. In this lesson, we will learn how to use the map function to alter all elements in a similar manner.

First Solve For One Element

Imagine that we have a list of names.

names = ['Homer', 'Marge', 'Bart', 'Maggie', 'Lisa']

We would like to add the name Simpson to the end of each name. Just as we did with filter, we can start by writing a function that solves the problem for one element.

def add_simpson(name):
    return name + " Simpson"
add_simpson("Homer")
'Homer Simpson'

Great, our method add_simpson successfully takes in a string, name, and returns that string with the added last name, Simpson.

Then Solve For All

Now we can iterate through the names one by one, and for each name we perform the same operation -- add the last name "Simpson".

def add_simpsons(elements):
    altered = []
    for element in elements:
        altered.append(add_simpson(element))
    return altered
simpsons = add_simpsons(names)
simpsons
['Homer Simpson',
 'Marge Simpson',
 'Bart Simpson',
 'Maggie Simpson',
 'Lisa Simpson']

So notice that unlike what we saw when using filter to select elements based on some conditional, there is no if statement in this for loop. Instead, the number of elements in our output list is the same as the number of elements in our input list. However, each one of those elements has been altered.

Finding what's common

As you may have guessed, using a for loop to alter each element by applying some operation is a common procedure in programming. Let's write a function that derives the initials of each person's name.

def find_initial(name):
    names = name.split(' ')
    first_name = names[0]
    last_name = names[1]
    return first_name[0] + last_name[0]
homer = simpsons[0]
find_initial(homer)
'HS'
def find_initials(elements):    
    altered = []
    for element in elements:
        altered.append(find_initial(element))
    return altered

find_initials(simpsons)
['HS', 'MS', 'BS', 'MS', 'LS']

Our two functions, are quite similar.

def add_simpsons(elements):
#     altered = []
#     for element in elements:
        altered.append(add_simpson(element))
#     return altered

def find_initials(elements):    
#     altered = []
#     for element in elements:
        altered.append(find_initial(element))
#     return altered

Map Function

The map function allows us to apply the same operation to each element and returns a new list of elements that have been modified by the operation.

The map function is used like this:

map(Function, Sequence)

Let's use the map function with add_simpson defined above.

map(add_simpson, names)
<map at 0x10d9d0748>

However, just as the filter function returns a filter object, the map function returns a map object. So, in order to get our desired list back, we need to coerce the map object to a list:

list(map(add_simpson, names))
['Homer Simpson',
 'Marge Simpson',
 'Bart Simpson',
 'Maggie Simpson',
 'Lisa Simpson']

Similarly, we can pass simpsons and find_initial to map and get the desired output as shown below.

list(map(find_initial, simpsons))
['HS', 'MS', 'BS', 'MS', 'LS']

So the map function goes through each element and executes the altering function on the current element. Then the return value of the altering function is stored in a map object. The desired output is obtained by coercing the map object to a list.

map, just like filter, is built into python and is always available. It is also computationally more efficient than a manually-coded for loop.

map can be used in more advance ways e.g. given multiple sequence arguments, it sends items taken from sequences in parallel as distinct arguments to the function. Let's see this through in the example below.

The pow built-in python function takes in two numbers as arguments and calculates the result by raising the first number to the power of the second number. Let's see this in action.

pow(2,4)
16

So 2 to the 4th power is 16 as shown above. map allows us to pass the pow function along with two lists as arguments to calculate the raise the elements from first list to the powers of the elements of second list as shown below:

list(map(pow, [2, 4, 8], [3, 5, 7]))
[8, 1024, 2097152]

This shows that with multiple sequences, map expects an N-argument function for N sequences i.e. pow requires two arguments and map uses two lists for mapping elements.

Summary

In this section, we learned about the map function which takes in two arguments. The first argument is the altering function, which operates on each element by passing through the element as an argument and returning a value. The second argument is the list of elements to be iterated through and operated on. The return values of the altering function are appended to a new list, which is returned after we coerce our map object into a list.

map-readme-python-al's People

Contributors

jeffkatzy avatar cutterbuck avatar tkoar avatar

Watchers

James Cloos avatar  avatar Mohawk Greene avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar  avatar  avatar Ben Oren avatar Matt avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Dominique De León avatar  avatar Lisa Jiang avatar Vicki Aubin avatar Maxwell Benton avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.