Hana K
Hana K

Reputation: 19

Is there a way to append in a 2d array without numpy?

So, my computer won't load numpy and I need to append another row to a 2d array with 7 rows. Is there a way to append another row?

I've already tried a.append but it doesn't work because there are multiple rows.

a = ([['Mon', 18, 20, 22, 17], 
     ['Tue', 11, 18, 21, 18],
     ['Wed', 15, 21, 20, 19], 
     ['Thu', 11, 20, 22, 21],
     ['Fri', 18, 17, 23, 22], 
     ['Sat', 12, 22, 20, 18],
     ['Sun', 13, 15, 19, 16]])

for elem in a:
        print(elem)

m_r = append(a, [['Avg', 12, 15, 13, 11]], 0)

I want to have another row of [['Avg', 12, 15, 13, 11]], 0) underneath the rest of my code, but I'm only getting errors.

Upvotes: 0

Views: 1739

Answers (2)

Yaniv
Yaniv

Reputation: 829

If I understand correctly, you have a list of lists, which you think of as a "2d array". To add another "row", simply use append, e.g. a.append(['Avg', 12, 15, 13, 11]).

Upvotes: 0

Dominik Wosiński
Dominik Wosiński

Reputation: 3864

append should normally work in this case. Try:

a.append(['Avg', 12, 15, 13, 11])

The problem might be the double bracket [[.

Upvotes: 1

Related Questions