List with duplicated values and suffix

前端 未结 6 1456
抹茶落季
抹茶落季 2020-11-30 02:48

I have a list, a:

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

and need to duplicate some values with the suffix _ind added this way (

6条回答
  •  春和景丽
    2020-11-30 03:38

    It can be shortened a little bit by moving the options to the inner for loop in the list comprehension:

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

提交回复
热议问题