Python - can a dict have a value that is a list?

∥☆過路亽.° 提交于 2019-12-04 02:22:17

Yes. The values in a dict can be any kind of python object. The keys can be any hashable object (which does not allow a list, but does allow a tuple).

You need to use [], not {} to create a list:

{ keyName1 : value1, keyName2: value2, keyName3: [val1, val2, val3] }
GreenMatt

Yes, it's possible:

d = {}
d["list key"] = [1,2,3]
print d

output:

{'list key': [1, 2, 3]}

It definitely can have a list and any object as value but the dictionary cannot have a list as key because the list is mutable data structure and keys cannot be mutable else of what use are they.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!