Title: Applying Functions To List Items
Slug: applying_functions_to_list_items
Summary: Applying Functions To List Items
Date: 2016-05-01 12:00
Category: Python
Tags: Basics
Authors: Chris Albon
In [23]:
regimentNames = ['Night Riflemen', 'Jungle Scouts', 'The Dragoons', 'Midnight Revengence', 'Wily Warriors']
In [26]:
# create a variable for the for loop results
regimentNamesCapitalized_f = []
# for every item in regimentNames
for i in regimentNames:
# capitalize the item and add it to regimentNamesCapitalized_f
regimentNamesCapitalized_f.append(i.upper())
# View the outcome
regimentNamesCapitalized_f
Out[26]:
In [25]:
capitalizer = lambda x: x.upper()
In [18]:
regimentNamesCapitalized_m = list(map(capitalizer, regimentNames)); regimentNamesCapitalized_m
Out[18]:
In [22]:
regimentNamesCapitalized_l = [x.upper() for x in regimentNames]; regimentNamesCapitalized_l
Out[22]: