List with duplicated values and suffix

Viewed 2476

I have a list, a:

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

and need to duplicate some values with the suffix _ind added this way (order is important):

['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']

I tried:

b = [[x, x + '_ind'] for x in a]
c = [item for sublist in b for item in sublist]
print (c)
['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']

Is there some better, more pythonic solution?

6 Answers

Since you asked for "simple", I thought I'd throw this in (albeit, maybe not the pythonic way):

for i in mylist: 
    mylist1.append(i);
    mylist1.append(i + '_ind');
Related