Prerequisite: List in Python
As we know Array is a collection of items stored at contiguous memory locations. In Python, a List (Dynamic Array) can be treated as an Array. In this article, we will learn how to initialize an empty array of some given size. Let’s see different Pythonic ways to create an empty list in Python with a certain size.
Method 1: Initialize empty array using * Operator
In this example, we are creating different types of empty using an asterisk (*) operator.
Python3
a = [ 0 ] * 10
print ( "Creating empty list of zeros: " , a)
b = [ None ] * 8
print ( "Creating empty list of None: " , b)
c = [[ 0 ] * 4 ] * 3
print ( "Creating 2D empty list of zeros: " , c)
d = []
print ( "Creating empty list of zeros: " , d)
|
Output:
Creating empty list of zeros: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Creating empty list of None: [None, None, None, None, None, None, None, None]
Creating 2D empty list of zeros: [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Creating empty list of zeros: []
Method 2: Create an empty list in python with a certain size using list comprehension
In this example, we are using Python List comprehension for 1D and 2D empty arrays.
Python3
a = [ 0 for x in range ( 10 )]
print (a)
b = [[ 0 ] * 4 for i in range ( 3 )]
print (b)
|
Output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Method 3: Create an empty list of a specific size in Python using a loop
In this example, we are using a Python loop for 1D and 2D empty arrays.
Python3
b = []
for x in range ( 5 ):
b.append([[]])
print (b)
c = []
for x in range ( 5 ):
c.append( 0 )
print (c)
|
Output:
[[[]], [[]], [[]], [[]], [[]]]
[0, 0, 0, 0, 0]
Method 4: Initialize empty array using Numpy
In this method, we are using the Numpy module to generate an empty matrix of 1D and 2D sizes using np.empty().
Python3
import numpy
a = numpy.empty( 5 , dtype = object )
print (a)
matrix = numpy.empty(shape = ( 2 , 5 ), dtype = 'object' )
print (matrix)
|
Output:
[None None None None None]
[[None None None None None]
[None None None None None]]
Method 5: Initialize empty array using repeat()
In this method, we can use the repeat() function from the itertools module to create a new iterator that returns the same value a certain number of times. This can be used to create an empty array of a specific size by providing a value of None and the desired length.
Python3
import itertools
a = list (itertools.repeat( 0 , 10 ))
print (a)
b = [ list (itertools.repeat( 0 , 4 )) for i in range ( 3 )]
print (b)
|
Output
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
This method has a time complexity of O(n) and an auxiliary space of O(n) where n is the desired length of the array.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
27 Mar, 2023
Like Article
Save Article