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

前端 未结 3 1825
甜味超标
甜味超标 2020-12-17 16:14

When using Python is it possible that a dict can have a value that is a list?

for example, a dictionary that would look like the following (see KeyName3\'s values):<

3条回答
  •  暖寄归人
    2020-12-17 16:29

    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] }
    

提交回复
热议问题