Reputation: 91
I have to add string --foo to each element in given set and been trying hard to do that but unable to do. Is it really possible to do that? Below is the set
a = {"apple", "banana", "cherry", "6363738", "1"}
output
a = {"apple--foo", "banana--foo", "cherry--foo", "6363738-foo", "1-foo"}
Upvotes: 0
Views: 138
Reputation: 16772
For a change, using lambda
:
>>> map(lambda x: x + '--foo', a)
OUTPUT:
>>> set(map(lambda x: x + '--foo', a)) # {'apple--foo', '6363738--foo', '1--foo', 'cherry--foo', 'banana--foo'}
Upvotes: 0
Reputation: 418
Several ways to accomplish this, here is a simple for loop:
for i in range(len(a)):
a[i] += "--foo"
Upvotes: 0
Reputation: 4472
You can try
a = {"apple", "banana", "cherry", "6363738", "1"}
{"{}--foo".format(i) for i in a}
or for Python 3.6 and above
{f"{i}--foo" for i in a}
Output
{"apple--foo", "banana--foo", "cherry--foo", "6363738-foo", "1-foo"}
Upvotes: 3
Reputation: 117866
You can use string concatenation in a set comprehension
>>> {i+'--foo' for i in a}
{'banana--foo', '6363738--foo', 'apple--foo', 'cherry--foo', '1--foo'}
Upvotes: 6