如何使用python在列表列表中添加项目 [英] how to add a item in a list of list using python

查看:35
本文介绍了如何使用python在列表列表中添加项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there any way to add a item to a list of list using python.

例如有一个列表如下:

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

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

I want to add a item '444' for the position test_list[2][1].

最后的列表应该是:

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

推荐答案

是的,您绝对可以做到.

Yes that is definitely something you can do.

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

You simply access the list object that you want to add another item to by its index, and then call .append() on that list object with the new value.

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

这篇关于如何使用python在列表列表中添加项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆