Python - Ways to Create a Dictionary of Lists
# Python Ways to Create a Dictionary of Lists
Python offers multiple ways to create a dictionary of lists. Dictionaries are a great data structure to store and work with key-value pairs of data. A dictionary of lists is a data structure that allows you to store multiple lists under one single dictionary. It is useful for organizing data, such as for storing student grades for multiple classes, or for creating a list of lists that can be sorted and searched. This article covers various techniques to create a dictionary of lists in Python.
Creating a dictionary of lists can be done in multiple ways. The most basic way to create a dictionary of lists is by using the `dict()` function. The `dict()` function takes in a list of tuples, where each tuple consists of a key and a value. The key is used to access the value and the value can be any type of data, including a list. For example, if we want to create a dictionary of lists that stores student information, we could do the following:
```
student_dict = dict([
('name', ['John', 'Sally', 'Sam', 'Jill']),
('age', [20, 21, 19, 22]),
('class', ['Computer Science', 'Business', 'Psychology', 'Art History'])
])
```
This creates a dictionary called `student_dict` that contains three lists: one for the student names, one for their ages, and one for their classes. We can now access the values by using the keys in the same way we access any other dictionary:
```
print(student_dict['name']) # Output: ['John', 'Sally', 'Sam', 'Jill']
print(student_dict['age']) # Output: [20, 21, 19, 22]
print(student_dict['class']) # Output: ['Computer Science', 'Business', 'Psychology', 'Art History']
```
Another way to create a dictionary of lists is by using the `dict.fromkeys()` method. This method takes in two arguments: a list of keys and a value for each key. The value for each key can be set to a list, creating a dictionary of lists. For example, if we want to create a dictionary of lists with the same student information as before:
```
student_dict = dict.fromkeys(['name', 'age', 'class'], [])
```
This will create a dictionary called `student_dict` with three empty lists as values. We can now add values to the lists by accessing the keys:
```
student_dict['name'] = ['John', 'Sally', 'Sam', 'Jill']
student_dict['age'] = [20, 21, 19, 22]
student_dict['class'] = ['Computer Science', 'Business', 'Psychology', 'Art History']
```
Finally, we can also use the `dict comprehension` syntax to create a dictionary of lists. This syntax allows us to loop over a list of keys and add values to each of the keys. For example, if we want to create a dictionary of lists for student information again, we can do the following:
```
student_dict = {key: [] for key in ['name', 'age', 'class']}
```
This will create a dictionary called `student_dict` with three empty lists as values. We can now add values to the lists by accessing the keys in the same way as before:
```
student_dict['name'] = ['John', 'Sally', 'Sam', 'Jill']
student_dict['age'] = [20, 21, 19, 22]
student_dict['class'] = ['Computer Science', 'Business', 'Psychology', 'Art History']
```
By using these methods, we can easily create a dictionary of lists in Python. Dictionaries of lists are a great way to store and organize data, so they can be useful in a wide variety of use cases.
Worried About Failing Tech Interviews?
Attend our webinar on
"How to nail your next tech interview" and learn
.png)
Hosted By
Ryan Valles
Founder, Interview Kickstart

Our tried & tested strategy for cracking interviews

How FAANG hiring process works

The 4 areas you must prepare for

How you can accelerate your learnings
Register for Webinar
Algorithm:
1. Create an empty dictionary by initializing a variable with curly braces: `dict = {}`
2. Create a list of elements for each key in the dictionary.
3. Assign each list to the corresponding key in the dictionary: `dict['key1'] = [list1]`
4. Repeat step 3 for each key in the dictionary.
5. Print the dictionary in markdown format: `print(str(dict))`
Sample Code:
```
dict = {}
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
dict['key1'] = list1
dict['key2'] = list2
print(str(dict))
```
Output: `{'key1': [1, 2, 3], 'key2': ['a', 'b', 'c']}`