Create list of single item repeated N times

前端 未结 6 1126
南笙
南笙 2020-11-22 03:57

I want to create a series of lists, all of varying lengths. Each list will contain the same element e, repeated n times (where n = len

6条回答
  •  遥遥无期
    2020-11-22 04:45

    Itertools has a function just for that:

    import itertools
    it = itertools.repeat(e,n)
    

    Of course itertools gives you a iterator instead of a list. [e] * n gives you a list, but, depending on what you will do with those sequences, the itertools variant can be much more efficient.

提交回复
热议问题