Home > OS >  How to define an empty 2 dimensional list instead of data = [["",""],["&quo
How to define an empty 2 dimensional list instead of data = [["",""],["&quo

Time:06-21

How to define an empty 2 dimensional list instead of data = [["",""],["",""],["",""],["",""]] For larger number of elements

CodePudding user response:

This is a possible solution:

data = [['' for j in range(m)] for i in range(n)]

m and n are the desired dimensions of your list of lists.

CodePudding user response:

A simple list comprehension will do:

data = [["", ""] for i in range(4)]

CodePudding user response:

   #initialize an empty 2d list
data = [[]]

#append to first list
data[0].append("a")
data[0].append("b")
data[0].append("c")

#append another list to the 2D list
data.append(['d','e','f'])

print(data)

this way you can make your rows or columns of any length

CodePudding user response:

lis = [[] for _ in range(3)] try it

  • Related