Create Dictionary of Lists in Python (2 Examples)
This article illustrates how to create a dictionary of lists in Python programming.
Table of contents:
If you want to know more about these contents, keep reading!
Example Data
Before all else, I’ll have to define some data that we can use in the examples below.
Subject = ["Math", "Science", "English", "History"] # first sample list Marks = [90, 85, 88, 80] # second sample list
As you can see, I have created list1
and list2
. Next, we will use list1
as the first value and list2
as the second value in a dictionary.
Let’s see how it is done!
Example 1: Form Dictionaries of Lists using Curly Brackets {}
In this example, I will demonstrate how to use the curly brackets {} to initialize a dictionary with list values.
dict1 = {"Subject" : ["Math", "Science", "English", "History"], # initialize dict1 "Marks" : [90, 85, 88, 80] } print(dict1) # print dict1 # {'Subject': ['Math', 'Science', 'English', 'History'], 'Marks': [90, 85, 88, 80]}
As you can see, now we have our lists embedded in a dictionary where the keys are the list names.
Next, we will take a look at how to populate a dictionary with lists using the dict() function.
Example 2: Form Dictionaries of Lists using dict() Function
In the second example, we will use the dict() function, where the keywords we provide will become the keys, and their corresponding arguments will become the values.
dict1 = dict(Subject = ["Math", "Science", "English", "History"], #initialize dict1 Marks = [90, 85, 88, 80] } print(dict1) # print dict1 # {'Subject': ['Math', 'Science', 'English', 'History'], 'Marks': [90, 85, 88, 80]}
Simple and easy, right?
If you are interested in how to append new elements to the lists in a dictionary, refer to my tutorial Append Element to List in Dictionary in Python.
Video & Further Resources
Do you need more info on the Python syntax of this article? Then I recommend watching the following video on my YouTube channel. I illustrate the topics of this article in the video.
The YouTube video will be added soon.
In addition, you might want to read some of the related Python tutorials on this homepage. Some tutorials are listed below.
- Append Element to List in Dictionary in Python
- Access Element in Lists within Dictionary in Python
- Convert Multiple Lists to Dictionary in Python
- The Python Programming Language
This tutorial has demonstrated how to form a dictionary of lists in the Python programming language. Let me know in the comments section, if you have additional questions.
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.