How to Convert a List of List to a Dictionary in Python?

For some applications, it’s quite useful to convert a list of lists into a dictionary.

  • Databases: List of list is table where the inner lists are the database rows and you want to assign each row to a primary key in a new dictionary.
  • Spreadsheet: List of list is two-dimensional spreadsheet data and you want to assign each row to a key (=row name).
  • Data Analytics: You’ve got a two-dimensional matrix (=NumPy array) that’s initially represented as a list of list and you want to obtain a dictionary to ease data access.

There are three main ways to convert a list of lists into a dictionary in Python (source):

  1. Dictionary Comprehension
  2. Generator Expression
  3. For Loop

Let’s dive into each of those.

1. Dictionary Comprehension

Problem: Say, you’ve got a list of lists where each list represents a person and consists of three values for the person’s name, age, and hair color. For convenience, you want to create a dictionary where you use a person’s name as a dictionary key and the sublist consisting of the age and the hair color as the dictionary value.

Solution: You can achieve this by using the beautiful (but, surprisingly, little-known) feature of dictionary comprehension in Python.

persons = [['Alice', 25, 'blonde'],
           ['Bob', 33, 'black'],
           ['Ann', 18, 'purple']]

persons_dict = {x[0]: x[1:] for x in persons}
print(persons_dict)
# {'Alice': [25, 'blonde'],
#  'Bob': [33, 'black'],
#  'Ann': [18, 'purple']}

Explanation: The dictionary comprehension statement consists of the expression x[0]: x[1:] that assigns a person’s name x[0] to the list x[1:] of the person’s age and hair color. Further, it consists of the context for x in persons that iterates over all “data rows”.

Exercise: Can you modify the code in our interactive code shell so that each hair color is used as a key and the name and age are used as the values?

Modify the code and click the “run” button to see if you were right!

2. Generator Expression

A similar way of achieving the same thing is to use a generator expression in combination with the dict() constructor to create the dictionary.

persons = [['Alice', 25, 'blonde'],
           ['Bob', 33, 'black'],
           ['Ann', 18, 'purple']]

persons_dict = dict((x[0], x[1:]) for x in persons)
print(persons_dict)
# {'Alice': [25, 'blonde'],
#  'Bob': [33, 'black'],
#  'Ann': [18, 'purple']}

This code snippet is almost identical to the one used in the “list comprehension” part. The only difference is that you use tuples rather than direct mappings to fill the dictionary.

3. For Loop

Of course, there’s no need to get fancy here. You can also use a regular for loop and define the dictionary elements one by one within a simple for loop. Here’s the alternative code:

persons = [['Alice', 25, 'blonde'],
           ['Bob', 33, 'black'],
           ['Ann', 18, 'purple']]

persons_dict = {}
for x in persons:
    persons_dict[x[0]] = x[1:]

print(persons_dict)
# {'Alice': [25, 'blonde'],
#  'Bob': [33, 'black'],
#  'Ann': [18, 'purple']}

Again, you map each person’s name to the list consisting of its age and hair color.

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!