Convert One Dimensional List into 2D in Python (2 Examples)


Hi! This short tutorial will show you 2 examples of how to turn a one-dimensional list into 2D in the Python programming language.

First, though, here is an overview of this tutorial:

Let’s dive into Python code!


Create Sample One Dimensional List

Here, we will create the sample one dimensional Python list that will be converted into a 2D list in this tutorial. Therefore, run the line of code below in your Python IDE to create the sample list:

oneD_list = ["Nduka", "Larry", "Chichi", "Leslie", "Donna", "Taiwo"]
# <class 'list'>

As shown, OneD_list is a list containing 6 strings. Let’s now continue with the examples!


Example 1: Turn 1D List into 2D Using zip() Function

In this example, we will use Python’s built-in zip() function to transform the one dimensional list to a 2D list:

twoD_list = [list(x) for x in zip(oneD_list[0::2], oneD_list[1::2])]
# [['Nduka', 'Larry'], ['Chichi', 'Leslie'], ['Donna', 'Taiwo']]
# <class 'list'>

In the above example, we parsed two slices of the original list as arguments to the zip() function. The first slice contains every other element starting from the first element ['Nduka', 'Chichi', 'Donna'], and the second slice contains every other element starting from the second element ['Larry', 'Leslie', 'Taiwo'].

The zip() function creates pairs of elements from these two slices and returns an iterable of tuples. We convert each tuple to a list using the list() function and store the resulting lists in a new list called “twoD_list”. Now there, we have our 2D list!

Example 2: Turn 1D List into 2D Using NumPy Functions & Methods

In this second example, we will use a chain of functions and methods from Python’s NumPy library to reshape the one-dimensional list to be 2D. First, though, we need to install and import NumPy:

# install numpy
pip install numpy
# import numpy
import numpy as np

Next, we will use a chain of functions and methods to transform the one-dimensional list into 2D:

twoD_list = np.array(oneD_list).reshape(-1, 2).tolist()
# [['Nduka', 'Larry'], ['Chichi', 'Leslie'], ['Donna', 'Taiwo']]
# <class 'list'>

In the code above, we, first of all, converted the list into a NumPy array using the np.array() function, after which the array is reshaped into a 2D array, using the reshape() method, with -1 indicating that NumPy should calculate the appropriate size of the first dimension based on the total size of the array and the size of the second dimension. Thereafter, we converted the resulting matrix into a list with the tolist() method.

With that, we have demonstrated how to convert a one-dimensional list into 2D in Python. I hope you found this tutorial helpful!


Video, Further Resources & Summary

Do you need more explanations on how to transform a one-dimensional list into 2D 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 turn a one-dimensional list into 2D in Python.


The YouTube video will be added soon.


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 turn a one-dimensional list into 2D in Python. In case you have further questions, you may leave a comment below.


R & Python Expert Ifeanyi Idiaye

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

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.