Python: extend items in a list

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

the classic way could be:

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

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

print aa

      

however I want to know the best way so that I try:

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

      

and

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

      

but they do not give the most correct one. What are the best ways?

thank.

+3


source to share


2 answers


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



+4


source


use map

built-in function



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

      

+1


source







All Articles