# Convert 2D List to Dictionary in Python (3 Examples)

Hi! This tutorial will show you how to change a 2D list to a dictionary in the Python programming language.

First, here is a quick overview:

Letâ€™s jump into the Python code!

## Create Demo 2D List

Here, we will create the demo 2D list of strings and integers that will be transformed into a dictionary. So, in your favored Python IDE, run the line of code below to create the demo 2D list:

```my2d_list = [["apple", 2], ["banana", 4], ["orange", 6], ["pear",3]]

print(my2d_list)
# [['apple', 2], ['banana', 4], ['orange', 6], ['pear', 3]]

print(type(my2d_list))
# <class 'list'>```

We are now ready to see some examples of how to turn this 2D list to a dictionary.

## Example 1: Change 2D List to Dictionary Using dict() Function

In this first example, we will use Pythonâ€™s built-in dict() function to change the 2D list to a dictionary:

```dictionary = dict(my2d_list)

print(dictionary)
# {'apple': 2, 'banana': 4, 'orange': 6, 'pear': 3}

print(type(dictionary))
# <class 'dict'>```

Parsing the 2D list to the `dict()` function turns it into a dictionary of key-value pairs.

## Example 2: Change 2D List to Dictionary Using Dictionary Comprehension

In this next example, we will use dictionary comprehension to turn the 2D list to a dictionary:

```dictionary = {x[0]: x[1] for x in my2d_list}

print(dictionary)
# {'apple': 2, 'banana': 4, 'orange': 6, 'pear': 3}

print(type(dictionary))
# <class 'dict'>```

The dictionary comprehension method runs an iteration through the 2D list inside curly brackets {}, and assigns the first item in each sub-list as the key and the second item as the value.

## Example 3: Change 2D List to Dictionary Using For Loop and setdefault() Method

In this final example, we will use a for loop and the setdefault() method to transform the 2D list to a dictionary:

```dictionary = dict()

for i,j in my2d_list:
dictionary.setdefault(i,j)

print(dictionary)
# {'apple': 2, 'banana': 4, 'orange': 6, 'pear': 3}

print(type(dictionary))
# <class 'dict'>```

In the above example, we first created an empty dictionary object; then we used the for loop to parse the first and second elements in each sub-list of the 2D list to the `.setdefault()` method, which formed key-value pairs for the dictionary.

With that, we have demonstrated 3 simple methods of converting a 2D list to a dictionary in Python. I hope you found it helpful!

## Video, Further Resources & Summary

Do you need more explanations on how to convert a 2D list to a dictionary in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to convert a 2D list to a dictionary in Python.

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to convert a 2D list to a dictionary in Python. In case you have further questions, you may leave a comment below.

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyiâ€™s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

Subscribe to the Statistics Globe Newsletter