title | author | date |
---|---|---|
Applying Functions To List Items |
Damian Mingle |
05/05/2018 |
philosopherNames = ['Aristotle', 'Plato', 'Socrates', 'Rene Descartes', 'Albert Camus']
# create an object to hold loop results
philosopherNamesCapitalized_loop = []
# for every item in philosopherNames
for i in philosopherNames:
# capitalize the item and add it to philosopherNamesCapitalized_loop
philosopherNamesCapitalized_loop.append(i.upper())
# View all results
philosopherNamesCapitalized_loop
['ARISTOTLE', 'PLATO', 'SOCRATES', 'RENE DESCARTES', 'ALBERT CAMUS']
capitalizeThis = lambda x: x.upper()
Map the capitalizeThis function to philosopherNames, convert the map into a list, and view the variable
philosopherNamesCapitalized_map = list(map(capitalizer, philosopherNames))
philosopherNamesCapitalized_map
['ARISTOTLE', 'PLATO', 'SOCRATES', 'RENE DESCARTES', 'ALBERT CAMUS']
philosopherNamesCapitalized_lc = [x.upper() for x in philosopherNames]
philosopherNamesCapitalized_lc
['ARISTOTLE', 'PLATO', 'SOCRATES', 'RENE DESCARTES', 'ALBERT CAMUS']