user4989939
user4989939

Reputation: 349

Changing list of strings using python 2x

Is it possible to change the string list- and add letter 'y' to each element in the list:

lis = ['dan','jim','roky']

Upvotes: 0

Views: 35

Answers (2)

Mridul Birla
Mridul Birla

Reputation: 124

lis = ['dan','jim','roky']
p=map(lambda x: x+'y',lis)

Map is a built in function which takes first argument as the function and each of the next arguments are the iterator on which you want to iterate and update the value and returns list of result.

The lambda function is anonymous function which updates the value of elements lis.

For more info visit https://docs.python.org/2/library/functions.html

Upvotes: 1

Yu Hao
Yu Hao

Reputation: 122383

Use list comprehension:

>>> [x + 'y' for x in lis]
['dany', 'jimy', 'rokyy']

Upvotes: 2

Related Questions