Open In App

Combine Two Flat Lists into a 2D Array

Last Updated : 19 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Combining two flat lists into a 2D array is a common task in programming. In this article, we will explore simple methods to achieve this goal. Whether we are working with Python or any other programming language, these methods can be adapted to suit our needs.

Using Zip ()

zip() function combines two lists of equal length by pairing corresponding elements. It creates an iterator, minimizing memory usage while merging the lists.

Example:

Python
a = ["John", "Jane", "Doe"]
b = [28, 24, 32]

# Combine lists using zip()
# convert the pairs into a list of lists
res = [list(x) for x in zip(a, b)]  

print(res)

Output
[['John', 28], ['Jane', 24], ['Doe', 32]]

Explanation:

  • zip(a, b) pairs the elements from lists a and b element by element.
  • The list() function converts each pair into a list.

Let's explore more methods To Combine Two Flat Lists Into A 2D Array in Python.

Using NumPy

NumPy provides a fast way to combine lists, especially for numerical data and large datasets. Its optimized array operations ensure quick execution and low memory usage.

Example:

Python
import numpy as np  
a = [1, 2, 3]
b = [4, 5, 6]

# Combine the lists into a 2D NumPy array
res = np.array([a, b])  
print(res)

Output
[[1 2 3]
 [4 5 6]]

Explanation:

  • np.array([a, b]) converts the lists a and b into a 2D NumPy array, where each list becomes a row.
  • This method is highly efficient for numerical data and large datasets due to NumPy’s optimized operations.

Using List Wrapping

This method combines lists by simply placing each list as an element inside a new list. It's fast and straightforward, making it ideal for small datasets.

Example:

Python
a = [1, 2, 3]
b = [4, 5, 6]

# Wrapping each list inside another list
res = [a, b]  
print(res)

Explanation:

  • This code combines two lists by placing each as an element inside a new list.
  • Result is a 2D list where each original list becomes a row.

Combining Lists with zip_longest()

itertools.zip_longest() combines lists of different lengths, filling the shorter list with a specified value, like None. This method is efficient and avoids creating extra structures.

Python
from itertools import zip_longest
a = [1, 2, 3]
b = [4, 5]

# Combine lists and fill missing values with None
res = [list(x) for x in zip_longest(a, b, fillvalue=None)]  
print(res)

Explanation:

  • zip_longest(a, b, fillvalue=None) pairs elements from a and b.
  • If a list is shorter, it fills the missing positions with None.
  • Result is a 2D list with paired elements, and None for missing values.

Using loop

Using loop combines two lists by iterating through each element and appending pairs. It's flexible but less efficient compared to methods like zip() or NumPy.

Example:

Python
a = [1, 2, 3]
b = [4, 5, 6]

# Loop through both lists and create pairs
res= []
for i in range(len(a)):
    res.append([a[i], b[i]]) 
print(res)

Output
[[1, 4], [2, 5], [3, 6]]

Explanation:

  • The loop iterates over the lists a and b, creating pairs at each index.
  • Each pair is added to the list res, forming a 2D list.

Next Article
Article Tags :
Practice Tags :

Similar Reads