Combine Two Lists into 2D Array in Python (3 Examples)
Hi! This tutorial will show you how to merge 2 lists into a 2D array in the Python programming language.
Here is an overview:
Let’s get right into the Python code!
Create Example Lists
So, in your preferred Python IDE, run the lines of code below to create the example Python lists:
list1 = [1, 2, 3] print(list1) # [1, 2, 3] print(type(list1)) # <class 'list'> list2 = [4, 5, 6] print(list2) # [4, 5, 6] print(type(list2)) # <class 'list'>
With the Python lists created, we will now examine two ways to join both lists together as a 2D array.
Example 1: Merge 2 Lists into a 2D Array Using list() & zip() Functions
array_2d = list(zip(list1, list2)) print(array_2d) # [(1, 4), (2, 5), (3, 6)] print(type(array_2d)) # <class 'list'>
zip() function is used to pair corresponding elements from both lists.
The resulting pairs are then converted into a list using the
list() function. Then, the contents of
array_2d are printed to the console using the print() function.
Although the output is not exactly a 2D array, it is considered such by some sources.
Example 2: Merge 2 Lists into a 2D Array Using List Comprehension
In this next example, we will use list comprehension to combine both lists into a 2D array:
array_2d = [[list1[i], list2[i]] for i in range(len(list1))] print(array_2d) # [[1, 4], [2, 5], [3, 6]] print(type(array_2d)) # <class 'list'>
In the above example, we used list comprehension to iterate through the elements in each list, and stored those elements inside sublists.
This action, thus, returned a 2D array of 3 rows and 2 columns, i.e. a 3 x 2 array.
Example 3: Merge 2 Lists into a 2D Array Using for Loop & zip() Function
In this last example, we will use a for loop and the built-in
zip() function to consolidate both lists into a 2D array:
array_2d =  for i in zip(list1,list2): array_2d.append(list(i)) print(array_2d) # [[1, 4], [2, 5], [3, 6]] print(type(array_2d)) # <class 'list'>
We then used the for loop to loop through the returned list of tuples, and parsed the loop variable to the
list() function, which then returned a 2D array of 3 rows and 2 columns.
Video, Further Resources & Summary
Do you need more explanations on how to merge 2 lists into a 2D array 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 merge 2 lists into a 2D array 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:
- count() Method for Lists in Python (2 Examples)
- Convert List of Tuples to List of Lists in Python (3 Examples)
- Change Index of Element in List in Python (Example)
- Append 2D List in Python (3 Examples)
- Learn Python Programming
This post has shown, using three examples, how to merge 2 lists into a 2D array in Python. Your use case will determine which method to adopt.
I do hope you found this tutorial helpful! 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