Skip to main content
 首页 » 编程设计

python之如何使用python在列表列表中添加项目

2025年05月04日15over140

有什么方法可以使用 python 将项目添加到列表列表中。

例如有一个列表如下:

test_list = [['abc','2'],['cds','333'],['efg']] 

我想为 test_list[2][1] 位置添加一个项目 '444'。

最后的列表应该是:

test_list = [['abc','2'],['cds','333'],['efg','444']] 

请您参考如下方法:

是的,这绝对是您可以做到的事情。

您只需通过其索引访问要添加另一个项目的列表对象,然后调用 .append()在具有新值的列表对象上。

test_list = [['abc','2'], ['cds','333'], ['efg']] 
test_list[2].append('444') 
# test_list is now: [['abc','2'], ['cds','333'], ['efg', '444']]