0

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

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

2 Answers 2

2

Use list comprehension:

>>> [x + 'y' for x in lis]
['dany', 'jimy', 'rokyy']
Sign up to request clarification or add additional context in comments.

Comments

1
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

2 Comments

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
@vaultah I have added the info. Can you update the vote ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.