How to create and fill a list of lists in a for loop

后端 未结 6 2099
轮回少年
轮回少年 2020-12-15 01:22

I\'m trying to populate a list with a for loop. This is what I have so far:

newlist = []
for x in range(10):
    for y in range(10):
        newlist.append(y         


        
6条回答
  •  猫巷女王i
    2020-12-15 01:54

    You can use this one line code with list comprehension to achieve the same result:

    new_list = [[i for i in range(10)] for j in range(10)]
    

提交回复
热议问题