R sapply() can be replaced by a list comprehension, but a fairly fair list comprehension does not allow you to strictly avoid loop writing .
In addition to map() you should take a look at Pandas , which provides Python alternatives to several functions that people use in R.
import pandas as pd vector = [1,2,3,4,5] square_vector = pd.Series(vector).apply(lambda x: x**2) print square_vector.tolist()
The above code leads to a new list with square imput values:
[1, 4, 9, 16, 25]
Here I passed the vector to the pd.Series(vector) series constructor and applied the anonymous apply(lambda x: x**2) function apply(lambda x: x**2) . The output is a series of pandas, which can be converted back to a list if tolist() is required. The pandas series has many features and is ideal for many data management and analysis tasks.
source share