list.extend and list comprehension

后端 未结 6 1801
北恋
北恋 2020-12-10 01:05

When I need to add several identical items to the list I use list.extend:

a = [\'a\', \'b\', \'c\']
a.extend([\'d\']*3)

Result



        
6条回答
  •  南方客
    南方客 (楼主)
    2020-12-10 01:25

    If you prefer extend over list comprehensions:

    a = []
    for x, y in l:
        a.extend([x]*y)
    

提交回复
热议问题