Ayesha
Ayesha

Reputation: 33

Adding items to Python 2d list

I have a empty 2d list which i dont know the length of or no of rows.

Animals has some number of items passed from function call.

In function startDocumentary_day(), For every function call, I have to append this each incoming animal list as each row in documentary list.

Eg:Animals can have ['cow','tiger'], sometimes it can also be empty

  def startDocumentary_day():
       return documentary.append([])
  def add(documentary, animals):
       documentary[j for j in range(len(animals))].append(animals)
  
       

I tried this, But shows error, for this line 2 in add function. Note :initially documentary is empty.

Upvotes: 0

Views: 33

Answers (1)

Barmar
Barmar

Reputation: 782584

You need to loop over the documentary list, and append to each element.

def add(documentary, animals):
    for d in documentary:
        d.append(animals)

Upvotes: 1

Related Questions