Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drmingle/7f69e1e33a2aa0ce897f8f82f4bd4ec2 to your computer and use it in GitHub Desktop.
Save drmingle/7f69e1e33a2aa0ce897f8f82f4bd4ec2 to your computer and use it in GitHub Desktop.
title author date
Apply Operations Over Items In A List
Damian Mingle
05/04/2018

The map() method

# Create a list of patients who have expired
patientDeaths = [82, 913, 902, 90, 83, 189, 334, 267, 44]
# Create a function that updates all patient expirations by adding 100
def updated(x): 
    return x + 100
# Create a list that applies updated() to all elements of patientDeaths
list(map(updated, patientDeaths))
[182, 1013, 1002, 190, 183, 289, 434, 367, 144]

For x in y method

# Create a list of patient deaths
expirations = [482, 93, 392, 920, 813, 199, 374, 237, 244]
# Create an object where we will put the updated patientexpiration numbers
expirationsUpdated = []
# Create a function that for each item in expirations, adds 100
for x in expirations:
    expirationsUpdated.append(x + 100)
# View casualties variables
expirationsUpdated
[582, 193, 492, 1020, 913, 299, 474, 337, 344]

The lambda functions method

# Map the lambda function x() over patient expirations
list(map((lambda x: x + 100), expirations))
[582, 193, 492, 1020, 913, 299, 474, 337, 344]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment