Open In App

Combine Two Flat Lists into a 2D Array

Last Updated : 15 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Combining two flat lists into a 2D array is a common task in programming, especially when dealing with data manipulation and analysis. In this article, we will explore four generally used and simple methods to achieve this goal. Whether you are working with Python, JavaScript, or any other programming language, these methods can be adapted to suit your needs.

Example :

Input: test_list1 = [1, 4, 5, 6, 5]
test_list2 = [3, 5, 7, 2, 5]
Output: [[1, 3],[4, 5], [5, 7],[6,2],[5,5]]

How To Combine Two Flat Lists Into A 2D Array?

Below, are the methods of How To Combine Two Flat Lists Into A 2D Array in Python.

Combine Two Flat Lists Into A 2D Array Using Nested Loops

In this example, below code generates a 2D array (`result_array`) by combining elements from two lists (`list1` and `list2`) using nested loops, creating pairs of elements from each list and appending them to the final array. The result is the Cartesian product of the two lists.

Python3




list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
 
result_array = []
for item1 in list1:
    inner_array = []
    for item2 in list2:
        inner_array.append([item1, item2])
    result_array.extend(inner_array)
 
print(result_array)


Output

[[1, 'a'], [1, 'b'], [1, 'c'], [2, 'a'], [2, 'b'], [2, 'c'], [3, 'a'], [3, 'b'], [3, 'c']]

Combine Two Flat Lists Into A 2D Array Using List Comprehension

In this example, below code uses list comprehension to create a 2D array (`result_array`) by combining elements from two lists (`list1` and `list2`). Each inner list in the result array contains pairs of corresponding elements from both lists, resulting in the Cartesian product of the two lists.

Python3




list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
 
result_array = [[item1, item2] for item1 in list1 for item2 in list2]
 
print(result_array)


Output

[[1, 'a'], [1, 'b'], [1, 'c'], [2, 'a'], [2, 'b'], [2, 'c'], [3, 'a'], [3, 'b'], [3, 'c']]

Combine Two Flat Lists Into A 2D Array Using zip() Function

In this example, below code utilizes the `zip()` function to create a list of tuples (`result_array`) by combining corresponding elements from two lists (`list1` and `list2`). The result is a 2D array where each tuple represents a pair of elements from the original lists.

Python3




list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
 
result_array = list(zip(list1, list2))
 
print(result_array)


Output

[(1, 'a'), (2, 'b'), (3, 'c')]

Combine Two Flat Lists Into A 2D Array Using NumPy

In this example, below code uses NumPy to create a 2D array (`result_array`) by horizontally stacking two lists (`list1` and `list2`). Each column in the resulting array corresponds to one of the original lists, providing an efficient way to combine elements from both lists.

Python3




import numpy as np
 
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
 
result_array = np.column_stack((list1, list2))
 
print(result_array)


Output

[['1' 'a']
['2' 'b']
['3' 'c']]

Conclusion

Combining two flat lists into a 2D array can be achieved through various methods. The choice of method depends on factors such as readability, efficiency, and the programming language being used. Whether you opt for nested loops, list comprehension, the zip() function, or leverage external libraries like NumPy, understanding these approaches equips you with the flexibility to handle different scenarios in your programming projects.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads