# Initialize Matrix in Python

## Initialize Matrix in Python

Sometimes in the world of competitive programming, we need to initialize the matrix, but we don’t wish to do it in a longer way using a loop. We need a shorthand for this. This type of problem is quite common in dynamic programming domain. Let’s discuss certain ways in which this can be done.
Method #1 : Using List comprehension
List comprehension can be treated as a shorthand for performing this particular operation. In list comprehension, we can initialize the inner list and then extend this logic to each row again using the list comprehension.

 `# Python3 code to demonstrate ` `# initializing matrix` `# using list comprehension` ` ` `# Declaring rows` `N ``=` `5` ` ` `# Declaring columns` `M ``=` `4` ` ` `# using list comprehension ` `# to initializing matrix` `res ``=` `[ [ ``0` `for` `i ``in` `range``(M) ] ``for` `j ``in` `range``(N) ]` ` ` `# printing result ` `print``(``"The matrix after initializing : "` `+` `str``(res))`

Output

```The matrix after initializing : [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
```

Method #2 : Using list comprehension + “*” operator
This problem can also be simplified using the * operator which can slightly reduce the tedious way task is done and can simply use multiply operator to extent the initialization to all N rows.

 `# Python3 code to demonstrate ` `# initializing matrix` `# using list comprehension` `# and * operator` ` ` `# Declaring rows` `N ``=` `5` ` ` `# Declaring columns` `M ``=` `4` ` ` `# using list comprehension ` `# to initializing matrix` `res ``=` `[ [``0` `for` `i ``in` `range``(M)]] ``*` `N` ` ` `# printing result ` `print``(``"The matrix after initializing : "` `+` `str``(res))`

Output

```The matrix after initializing : [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
```

Method #3 : Using + “*” operator twice
Similar to above example we can also initialize the columns using “*” operator twice.

 `# Python3 code to demonstrate ` `# initializing matrix` `# * operator twice` ` ` `# By: Pushpak Jalan, Tezpur University` ` ` `# Declaring rows` `N ``=` `5` ` ` `# Declaring columns` `M ``=` `4` ` ` `# Using * operator twice to initialize matrix` `res ``=` `[[``0``] ``*` `M] ``*` `N` ` ` `# printing result ` `print``(``"The matrix after initializing : "` `+` `str``(res))`

Output

`The matrix after initializing : [[0, 0, 0, 0], [0, 0, 0, 0], [0,`

Last Updated on March 1, 2022 by admin

## sys.stdout.write in Pythonsys.stdout.write in Python

sys.stdout.write in Python This is a built-in Python module that contains parameters specific to the

## numpy.random.randn() in Pythonnumpy.random.randn() in Python

numpy.random.randn() in Python The numpy.random.randn() function creates an array of specified shape and fills it with random

## Beautifulsoup Installation – PythonBeautifulsoup Installation – Python

Beautifulsoup Installation – Python Beautiful Soup is a Python library for pulling data out of

## Python bytearray() functionPython bytearray() function

Python | bytearray() function bytearray() method returns a bytearray object which is an array of given

## Python: os.path.abspath() method with examplePython: os.path.abspath() method with example

Python: os.path.abspath() method with example OS module in Python provides various methods for interacting with the

## Keras.Conv2D ClassKeras.Conv2D Class

Keras.Conv2D Class Keras Conv2D is a 2D Convolution Layer, this layer creates a convolution kernel that

## Python strip() methodPython strip() method

Python String | strip() The strip() method in-built function of Python is used to remove all the

## Data type Object (dtype) in NumPy PythonData type Object (dtype) in NumPy Python

Data type Object (dtype) in NumPy Python Every ndarray has an associated data type (dtype)