Creating a list in Python with multiple copies of a given object in a single line

前端 未结 6 1167
孤街浪徒
孤街浪徒 2020-12-03 04:56

Suppose I have a given Object (a string \"a\", a number - let\'s say 0, or a list [\'x\',\'y\'] )

I\'d like to create list containing many copies of thi

6条回答
  •  既然无缘
    2020-12-03 05:22

    In case you want to create a list with repeating elements inserted among others, tuple unpacking comes in handy:

    l = ['a', *(5*['b']), 'c']
    l
    Out[100]: ['a', 'b', 'b', 'b', 'b', 'b', 'c']
    

    [the docs]

提交回复
热议问题