问答详情
源自:5-5 Python向list添加新的元素

该怎么把里面的中括号去掉?

d=['Alice','Bob','Candy','David','Ellena']

l=['Gen','Phoebe','Zeo']

d.append(l)

print(d)  ==>['Alice', 'Bob', 'Candy', 'David', 'Ellena', ['Gen', 'Phoebe', 'Zeo']]



提问者:慕前端0137584 2021-03-08 14:55

个回答

  • 慕无忌1397218
    2021-04-07 09:59:51

    用extend,extend是将两个list合并成一个list。append()是将你传入的参数看做一个元素,拼到 上一个list中

    d=['Alice','Bob','Candy','David','Ellena']

    l=['Gen','Phoebe','Zeo']

    d.extend(l)

    print(d)


  • 武哥知事
    2021-03-09 11:55:06

    d=['Alice','Bob','Candy','David','Ellena']

    l=['Gen','Phoebe','Zeo']

    for x in l:

        d.append(x)

    print(d)