Create Dictionary with List as Value in Python (2 Examples)

 

In this Python tutorial you’ll learn how to create a dictionary where the values are lists.

The tutorial consists of two examples for the dictionary with lists. To be more specific, the content looks as follows:

Let’s get started!

 

Creation of Example Data

Assume that we have two string lists, as one containing fruit names, the other containing vegetable names.

["apple", "banana"]                                                      # first sample list
["carrot", "potato"]                                                    # second sample list

In the next example, we will use these lists as values in a dictionary labeling them. Without further ado, let’s jump into the example!

 

Example 1: Create Dictionary with Lists as Values using Basic Approach

In the basic approach, we will use curly {} brackets to form our dictionary, which will be called as my_dict.

my_dict = {"fruits": ["apple", "banana"], "vegetables": ["carrot", "potato"]}  # create dictionary
 
print(my_dict)                                                                 # print my_dict
# {'fruits': ['apple', 'banana'], 'vegetables': ['carrot', 'potato']}

The previous output shows that our dictionary using relevant keys is successfully created.

Alternatively, the dict() function could have also been used as a basic approach. To see how it is used, check the tutorial Create Dictionary of Lists in Python.

 

Example 2: Create Dictionary with Lists as Values using Subscript

Alternatively, one can first create an empty dictionary, then add the key-value pairs by subscript.

my_dict2 = {}                                                                  # create empty dictionary
 
my_dict2["fruits"] = ["apple", "banana"]                                       # add the first key-value pair
my_dict2["vegetables"] = ["carrot", "potato"]                                  # add the second key-value pair
 
print(my_dict2)                                                                # print my_dict2
{'fruits': ['apple', 'banana'], 'vegetables': ['carrot', 'potato']}

As you can see above, first, we assigned the fruits’ list to the key "fruits", later we have assigned the vegetables’ list to the key "vegetables". If you wonder how you can extend these lists using subscripts, then see my tutorial, Append Element to List in Dictionary in Python.

 

Video, Further Resources & Summary

I have recently released a video on the Statistics Globe YouTube channel, which demonstrates the Python codes of this article. You can find the video below.

 

The YouTube video will be added soon.

 

In addition, you may have a look at the other tutorials on this website. We have published numerous articles on topics such as data inspection, data conversion, indices, and lists.

 

In summary: This article has explained how to form a dictionary with list values in Python programming. Please tell me about it in the comments section below, in case you have any further questions.

 

Cansu Kebabci R Programmer & Data Scientist

This page was created in collaboration with Cansu Kebabci. Have a look at Cansu’s author page to get more information about her professional background, a list of all his tutorials, as well as an overview on her other tasks on Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top