Python: fastest way to create a list of n lists

前端 未结 5 640
温柔的废话
温柔的废话 2020-11-28 20:37

So I was wondering how to best create a list of blank lists:

[[],[],[]...]

Because of how Python works with lists in memory, this doesn\'t

5条回答
  •  难免孤独
    2020-11-28 21:25

    To create list and list of lists use below syntax

         x = [[] for i in range(10)]
    

    this will create 1-d list and to initialize it put number in [[number] and set length of list put length in range(length)

    • To create list of lists use below syntax.
        x = [[[0] for i in range(3)] for i in range(10)]
    

    this will initialize list of lists with 10*3 dimension and with value 0

    • To access/manipulate element
        x[1][5]=value
    

提交回复
热议问题