How to Take 2D Array Input in Python

How to Take 2D Array Input in Python

A 2D array (two-dimensional array) is a data structure that stores data in the form of rows and columns. It is used to represent data in a grid format.

Python provides us a lot of ways to take a 2d array as input. In this blog, we will learn all of these methods in more detail.

Table of Contents:

Various ways to take 2D array input in Python?

1. Taking 2D Array Input Using Nested Loops in Python

To take a 2d array as input, we can use a nested loop. One for iterating the row, and the other for iterating the column. This is the easiest and most commonly used method by beginner developers.

Code:

rows = int(input("Enter number of rows: "))
cols = int(input("Enter number of columns: "))

matrix = []

for i in range(rows):  
    row = [] 
    for j in range(cols):  
        value = int(input(f"Enter element at position ({i},{j}): ")) 
        row.append(value) 
    matrix.append(row)  


print("2D Array (Matrix):")
for i in range(rows):  
    for j in range(cols):  
        print(matrix[i][j], end=" ")  
    print()

Output:

Nested Loops in Python output

Explanation: Here, we have run two for loops, one for iterating rows and the other for iterating columns. And stored the value in a matrix array. Also, we have printed the values using two loops.

2. Taking 2D Array Input Using List Comprehension in Python

We can use list comprehension, which makes the code more readable and simple. In Python, List is a data type in which we can store integers, floats, strings, etc. We can access the list items with the help of an index.

Code:

rows = int(input("Enter number of rows: "))
cols = int(input("Enter number of columns: "))

matrix = [[int(x) for x in input().split()] for _ in range(rows)]

print("2D Array (Matrix):")
for row in matrix:
    print(*row)

Output:

List Comprehension in Python output

Explanation: The above code takes rows of lines of space-separated values, where [int(x) for x in input().split()] converts each line into a list of integers. Finally, the outer list will convert the array into a 2d array(matrix).

3. Taking 2D Array Input Using NumPy in Python

NumPy provides powerful tools and methods for efficiently handling array operations. To take a 2d array as input using NumPy, we need to use the numpy.array() function to convert the input into a NumPy array.

Code:

import numpy as np

rows = int(input("Enter number of rows: "))

cols = int(input("Enter number of columns: "))

matrix = np.array([list(map(int, input().split())) for _ in range(rows)])

print("2D Array (Matrix):")

print(matrix)

Output:

Using NumPy in Python output

Explanation: The above code uses the list to take rows lines of space-separated input,  and it converts each line into a list of integers using map(int, input().split()). The np.array() function then converts the list of lists into a NumPy 2D array (matrix).

Conclusion

A 2d array is used to store data in the form of rows and columns. Matrix and Grid are examples of 2d array data structures. Here we have learned how we can take a 2d array as input in Python. Python provides us multiple ways to do this task. If you want to become a Python expert, you should refer to our Python course.

FAQs

1. How do you access an element in 2D array?

You can access elements in a 2D array using index of the row and column.

 Eg: matrix[row][col], Here row is the index of row, and col is the index of column.

2. What is a 2D array in Python?

A 2d array is a data structure that is used to represent the elements in the form of rows and columns.

3. How to create a 2D array in Python?

To create a 2d array in Python, we can use a list or numpy array(numpy.array()).

4. What is an array in Python?

An array in Python is a data structure that stores multiple values in an ordered way. We can access the elements with the help of an index.

5. How many types of arrays are there in Python?

There are 4 types of arrays in Python:

  • Fixed Size Array
  • Dynamic Sized Array
  • 1-Dimensional Array
  • Multi-Dimensional Array

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner