Skip to content

Python dictionary with the list as value | Example code

  • by

Python dictionary allows the use of a list as the value. That means you can define a dictionary with the list as value to a key in Python.

Must Read: Python dictionary same key multiple values

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

The values in a dict can be any kind of python object. The keys can be any hashable object.

Example dictionary with the list as the value in Python

Simple example code dictionary which has value as a list.

dict = {
    'C1': [10, 20, 30],
    'C2': [20, 30, 40]
}

print(dict)
print(type(dict))

Output:

Python dictionary with the list as value

Or

Add list as a value into a dictionary

d = {}

d["list key"] = [1, 2, 3]
print(d)

Output: {‘list key’: [1, 2, 3]}

Do comment if you have any doubts or suggestions on this Python dictionary topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *