# Convert 2D List into One Dimension in Python (3 Examples)

Hi! This tutorial will show you how to transform a 2D list into one dimension in the Python programming language.

First, though, here is a quick overview:

Let’s get right into the Python code!

## Create Sample 2D List

Here, we will create the sample 2D list of integers that will be turned into one dimension. Therefore, in your preferred Python IDE, run the line of code below to create the sample 2D list:

```my_2dlist = [[1,2,3,4],[5,6,7,8]]

print(my_2dlist)
# [[1, 2, 3, 4], [5, 6, 7, 8]]

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

Now, we are ready to see examples of changing the 2D list to a 1D list.

## Example 1: Transform 2D List to One Dimension Using itertools Module

In this first example, we will use Python’s built-in itertools module to transform the 2D list to one dimensional:

```# import module
import itertools

my_list = list(itertools.chain(*my_2dlist))

print(my_list)
# [1, 2, 3, 4, 5, 6, 7, 8]

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

In the above example, we called the chain() function, which can take a number of iterables and return one iterable. In the function, we parsed the 2D list along with the unpacking `*` operator and wrapped it up inside the list() function to return a one dimensional list.

## Example 2: Transform 2D List to One Dimension Using Nested For Loop

In this next example, we will turn the 2D list to one dimension using a nested for loop:

```my_list = []

for i in my_2dlist:
for j in i:
my_list.append(j)

print(my_list)
# [1, 2, 3, 4, 5, 6, 7, 8]

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

In the example above, we created an empty list called “my_list” and then used a nested for loop to iterate through the sublists in the 2D list and then through the individual elements in each sub-list. After all, they are appended to the initialized empty list using the append() method.

## Example 3: Transform 2D List to One Dimension Using List Comprehension

In this final example, we will use list comprehension to convert the 2D list to a 1D list:

```
my_list = [x for y in my_2dlist for x in y]

print(my_list)
# [1, 2, 3, 4, 5, 6, 7, 8]

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

The list comprehension method works exactly like the nested for loop in the previous example, but it is a more concise approach. In the above example, we first iterated through the sub-list elements in the 2D list (y); after that, we unpacked the elements in each sub-list inside my_list (x).

With that, we have demonstrated, with 3 simple examples, how to convert a 2D list to one dimension in Python. I hope you found this tutorial helpful!

## Video, Further Resources & Summary

Do you need more explanations on how to convert a 2D list to one dimension 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 one dimension 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 one dimension 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