How to apply a function to each element of a list without for loops or lambda functions?
Question:
For example, let’s say I have a list:
integer_list = [1, 2, 3, 4]
and I want to plug each element of the list into a formula, like (x*5)+1. Right now I am doing integer_list[0] * 5 + 1
How would I then get the answer from that and multiply every item in the set by it?
Please help!
Answers:
no lambda no cry
def f(x):
return (x*5)+1
result = list(map(f, integer_list))
For example, let’s say I have a list:
integer_list = [1, 2, 3, 4]
and I want to plug each element of the list into a formula, like (x*5)+1. Right now I am doing integer_list[0] * 5 + 1
How would I then get the answer from that and multiply every item in the set by it?
Please help!
no lambda no cry
def f(x):
return (x*5)+1
result = list(map(f, integer_list))