# building a list
mylist = []
for item in range(0,10):
mylist.append(item*2)
mylist
Run to view results
[item*2 for item in range(0,10)]
Run to view results
# Write a function that takes a string and prints every letter in that string. use a for loop.
mystring='abcde'
def myfunc(anystring):
answer=[]
for l in anystring:
answer.append(l)
return answer
myfunc(mystring)
Run to view results
# Rewrite this same function using list comprehension.
def myfunc(anystring):
return [l for l in anystring]
myfunc(mystring)
Run to view results
# try lambda
list(map(lambda l: l, mystring))
Run to view results
# Write a function that will receive any list of numbers,
# and add 5 to each element of that list. Use a for loop.
def myfunc(numlist):
nums_only=[]
if isinstance(numlist,list):
for i in numlist:
if isinstance(i,(int,float)):
nums_only.append(i+5)
else:
pass
else:
print('please provide a list!')
return nums_only
myfunc([3,4,5,6,'bear',7,2,8])
Run to view results
# Rewrite this same function using list comprehension.
def myfunc(numbers):
return [i + 5 if isinstance(i,(int,float)) else i for i in numbers]
myfunc([5,4,7,'bear',2,4])
Run to view results
# try lambda
mylist = [5,4,7,'bear',2,4]
list(map(lambda i: i + 5 if isinstance(i,(int,float)) else i, mylist))
Run to view results
# Import the numpy library.
import numpy as np
# Write a function that receives any list n and "Binarize" n so that
# any value greater than or equal to the mean of n is 1, otherwise 0.
def myfunc(n):
binary = []
for i in n:
if i >= np.mean(n):
binary.append(1)
else:
binary.append(0)
return binary
myfunc([4,5,6,1,2])
Run to view results
# Rewrite this same function using list comprehension.
def myfunc(n):
return [1 if x >= np.mean(n) else 0 for x in n ]
myfunc([4,5,6,1,2])
Run to view results
# try lambda
mylist = [4,5,6,1,2]
list(map(lambda x: 1 if x >= np.mean(mylist) else 0, mylist))
Run to view results
# Write a function to swap 1s to 0s and 0s to 1s. If the element is neither a 0 nor 1, make it None.
# Use a for loop.
def myfunc(n):
answer = []
for i in n:
if i == 0:
answer.append(1)
elif i == 1:
answer.append(0)
else:
answer.append(None)
return answer
myfunc([0,1,1,0,0,"a"])
Run to view results
# Rewrite this same function using a list comprehension.
def myfunc(n):
return [1 if x == 0 else 0 if x == 1 else None for x in n]
myfunc([0,1,1,0,0,"a"])
Run to view results
# try lambda
mylist = [0,1,1,0,0,"a"]
list(map(lambda x: 1 if x == 0 else 0 if x == 1 else None, mylist))
Run to view results
# Write a function that receives a list and gets the square and square root of all non-negative numbers in the list.
# Use a for loop.
def myfunc(n):
squared = []
squareroot = []
for i in n:
if i >= 0:
squared.append(i**2)
squareroot.append(i**0.5)
else:
squared.append(i)
squareroot.append(i)
return squared, squareroot
myfunc([4,-1,0,16.1])
Run to view results
# Rewrite this same function using a list comprehension.
def myfunc(n):
return [i**2 if i >= 0 else i for i in n], \
[i**0.5 if i >= 0 else i for i in n]
myfunc([4,-1,0,16.1])
Run to view results
# try lambda
mylist = [4,-1,0,16.1]
print(list(map(lambda x: x**2 if x >= 0 else x, mylist)))
print(list(map(lambda x: x**0.5 if x >= 0 else x, mylist)))
Run to view results
# Write a function that receives a list of Celsius temperatures and converts to Fahrenheit.
# The formula is (9/5)*temp + 32)
def myfunc(celsius):
return [((9/5)*temp + 32) for temp in celsius]
Run to view results
myfunc([0,10,20.1, 34.5])
Run to view results
# try lambda
mylist = [0,10,20.1, 34.5]
print(list(map(lambda temp: ((9/5)*temp + 32), mylist)))
Run to view results