Python

Python 2D Lists (arrays) Tutorial

Hello in this tutorial, we will see how to create a 2D list (arrays) in python programming.

1. Introduction

An array is a dynamic data structure that stores the data in a linear form. So to work with this tutorial we will use a list object to perform all the array-related operations. A two-dimensional array is an array within an array. The data position in a two-dimensional array is determined with the help of two indices known as rows and columns. It is represented by the syntax as –

Syntax

array_input = [ [d1, d2, .... dn], [e1, e2, .... en] ]

1.2 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

2. Python 2D Lists Tutorial

Let us dive in with the programming stuff now. I am using JetBrains PyCharm as my preferred IDE. You’re free to choose the IDE of your choice.

2.1 Creating an implementation file

Let us understand the implementation with the help of a python script. The script consists of 3 methods –

  • basic(…) – To create a 2D array via the conventional way
  • nested_generators(…) – To create a 2D array via the nested generator. Generators are the methods that generate a sequence of results instead of a single sequence
  • input_to_array(…) – To create a 2D array with the help of user input (to determine the Array size and accept input) and nested generator

Implementation file

# Python 2D list tutorial
# Syntax - array-name = [ [d1, d2, .... dn], [e1, e2, .... en] ]

# Generating 2D array via naive way
def basic(array_input):
    rows, cols = (3, 3)
    for i in range(rows):
        col = []
        for j in range(cols):
            col.append(0)
        array_input.append(col)

    print('2D array = {}\n'.format(array_input))


# Generating 2D array via nested generators
def nested_generators():
    rows, cols = (5, 5)
    # nested generators
    arr = [[0 for i in range(cols)] for j in range(rows)]

    print('2D array = {}\n'.format(arr))


# Generating 2D array via user input and nested generators
def input_to_array(array_input):
    # Asking the user input for the array size
    size = int(input('Enter the array size:'))
    for x in range(size):
        # Nested generators
        print('Enter array {}'.format(x + 1))
        array_input.append([int(y) for y in input().split()])

    print('\n2D array = {}'.format(array_input))


# Driver code
if __name__ == '__main__':
    basic(array_input=[])

    # nested_generators()

    # input_to_array(array_input=[])

print('\ndone')

You can uncomment the method of your choice and run this python script. If everything goes well the output will be shown in the IDE console.

Console output

2D array = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

2D array = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

Enter the array size:2
Enter array 1
1234
Enter array 2
5678

2D array = [[1234], [5678]]

done

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned –

  • Introduction to list in python programming for creating a two-dimensional array
  • Sample programming stuff

You can download the source code of this tutorial from the Downloads section.

4. Download the Project

This was a tutorial to create a two-dimensional array in python.

Download
You can download the full source code of this example here: Python 2D Lists tutorial

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button