如何在字典中追加列表? [英] How to append to a list in a dictionary?

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

问题描述

我可以附加到词典的列表中吗?

Can I append to a list in a dictionary?

test = {'food' : 'apple'}

是否存在添加'banana'并将其转换为

Is there a command to add 'banana' and turn it into

test = { 'food': ['apple','banana'] }

谢谢

推荐答案

您需要创建一个dict,其中values是列表:

You need to create a dict where the values are lists:

test = {'food' : ['apple']}
test['food'].append('banana')

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

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