Mark K
Mark K

Reputation: 9378

Python: to prolong elements in a list

I am trying to create a new list based on the old one, from ['a','b','c'] to ['a12', 'b12', 'c12'].

a classic way can be:

a = ['a','b','c']
aa = []

for item in a:
    aa.append(item + '12')

print aa

however I want to learn a better way so I tried:

aa = a.extend('12%s' % item for item in l[:])

and

aa = [item.join('12') for item in l]

but they don't produce the most correct one. what are the better ways?

thanks.

Upvotes: 3

Views: 91

Answers (2)

wRAR
wRAR

Reputation: 25569

aa = [item + '12' for item in a]

Upvotes: 4

levi
levi

Reputation: 22707

use map built-in function

a  = ['a','b','c']
aa = map(lambda x:x+"12",a)
print aa
>> ['a12', 'b12', 'c12']

Upvotes: 1

Related Questions