List with duplicated values and suffix

前端 未结 6 1465
抹茶落季
抹茶落季 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:37

    You can use itertools.chain():

    import itertools
    
    l = ['a','b','c']
    
    new_list = list(itertools.chain.from_iterable([[i, i+"_ind"] for i in l]))
    
    print new_list
    

    Output:

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

提交回复
热议问题