How to use list comprehension to express a loop in a single statement and apply a method to each element in Python

1 Answer

0 votes
def f(s):
    return "*" + s + "*"

data = ["python", "java", "php", "c++", "javascript"]

lst = [f(x) for x in data]

print(lst)


'''
run:

['*python*', '*java*', '*php*', '*c++*', '*javascript*']

'''

 



answered Oct 27, 2018 by avibootz
...