Python: Append item to list N times

后端 未结 6 1150
礼貌的吻别
礼貌的吻别 2020-12-12 16:46

This seems like something Python would have a shortcut for. I want to append an item to a list N times, effectively doing this:

l = []
x = 0
for i in range(1         


        
6条回答
  •  失恋的感觉
    2020-12-12 17:40

    Itertools repeat combined with list extend.

    from itertools import repeat
    l = []
    l.extend(repeat(x, 100))
    

提交回复
热议问题