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"] print(type(oneD_list)) # <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])] print(twoD_list) # [['Nduka', 'Larry'], ['Chichi', 'Leslie'], ['Donna', 'Taiwo']] print(type(twoD_list)) # <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'].
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() print(twoD_list) # [['Nduka', 'Larry'], ['Chichi', 'Leslie'], ['Donna', 'Taiwo']] print(type(twoD_list)) # <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
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:
- Convert List of Tuples to List of Lists in Python (3 Examples)
- Access Elements in Lists with Dictionary in Python (2 Examples)
- Convert Python List to PyTorch Tensor & Vice-Versa in Python (Examples)
- Learn Python Programming
- Convert List of Boolean to Float in Python (2 Examples)
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.
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.
Statistics Globe Newsletter