Home > Back-end >  How can I append a new list to an existing list?
How can I append a new list to an existing list?

Time:11-15

So I'm trying to add a new list to my original list of lists, aka grid, and here is my code,

board = [
    [" "," ","1"," "],
    [" "," ","1"," "],
    ["1","1"," "," "],
    ["1"," "," ","1"]]
def total1count(board: list):
    t = []
    for x in board:
        t.append(x.count("1"))
    return sum(t)
s = (list("1"*total1count(board)))
print(board.extend(s))

However, when I run the code, the output returns None... why is that??

CodePudding user response:

Extend just modifies the list, you cant print it.

board.extend(s)
print(board)

CodePudding user response:

Array.extend(Array2) did not return anything (means none) that's why the output is none. if you want to print board you need to run board.extend(s) first than print board

board = [
[" "," ","1"," "],
[" "," ","1"," "],
["1","1"," "," "],
["1"," "," ","1"]]
def total1count(board: list):
    t = []
    for x in board:
        t.append(x.count("1"))
    return sum(t)
s = (list("1"*total1count(board)))
board.extend(s)
print(board)
  • Related