Multi dimensional arrays in Python of a dynamic size

Viewed 64583

very new to python so attempting to wrap my head around multi dimensional arrays. I read the existing posts and most of them deal with multi dimensional arrays given dimensions. In my case, I do not have dimensions for the total number of rows possible. A file is being processed, which is CSV and has 7 columns, but each line, depending on meeting or failing a criteria is accordingly drafted into an array. Essentially each line has 7 columns, but the number of rows cannot be predicted. The line is being treated as a list.

My aim is to create a multidimensional array of eligible lines and then be able to access values in the array. how can I do this?

essentially, how do I tackle creating a 2D list:

list_2d = [[foo for i in range(m)] for j in range(n)]

The above creates an mxn sized list but in my case, I know only n (columns) and not m(rows)

6 Answers

You can even try this it worked for me

s = [[] for y in range(n)]

try below

#beg 

a=[[]]

r=int(input("how many rows "))
c=int(input("how many cols "))

for i in range(r-1):
    a.append([])

for i in range(r):
    print("Enter elements for row ",i+1)
    for j in range(c):
        num=int(input("Enter element "))
        a[i].append(num)

for i in range(len(a)):
     print()
     for j in range(len(a[i])):
         print(a[i][j],end="  ")

end

Related