22.4 C
New York
Wednesday, February 21, 2024
HomeProgrammingPythonHow to Create an Empty Array In Python

How to Create an Empty Array In Python

In this tutorial, you will learn how to create an empty array in Python. We have covered four different methods to create empty array using NumPy.

This article will discuss how to create an empty array in Python. We will also see different methods available to perform operations on the array.

An array is a data structure storing the same data type elements. In Python, we can create an array using the NumPy module.

Numpy stands for numeric Python used to process the arrays. We can create an array in numpy and reverse the array using some inbuilt functions available in numpy.

Syntax:

import numpy

Creation:

We can create an array through the numpy module.

Syntax:

numpy.array([elements])

We can also consider a list array in Python.

The list is a data structure in Python used to store elements with multiple data types. We can create a list by using [].

Also ReadHow to Create Python Empty Set

Method 1: Creating an empty NumPy array

#importing numpy module
import numpy

#create an empty numpy array
my_array=numpy.array([])

#display
print(my_array)

Output:

[]

Method 2: Creating an empty list array

#create an empty list
my_array=[]

#display
print(my_array)

Output:

[]

Also ReadHow To Create An Empty Dictionary in Python

Method 3: Create an empty list array with 0’s

In this example, we create a list with ten 0s and display the list and number of elements in the list using the len() function.

#create an  empty list
my_array=[0]*10

#display
print(my_array)

#get the length
print(len(my_array))

Output:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
10

Method 4: Create an empty list array with None values

In this example, we create a list with 10 None values and display the list and number of elements in the list using the len() function.

#create an  empty list
my_array=[None]*10

#display
print(my_array)

#get the length
print(len(my_array))

Output:

[None, None, None, None, None, None, None, None, None, None]
10

Python List Methods

1. append()

This method is used to add an element to a list.

Syntax:

my_array.append(element)

Where my_array is an input list, and element is the value to be added.

Example:

In this example, we will create an array with 10 elements and add a string value to the list.

#create an  list
my_array=[1,23,45,3,34,6,7,8,9,32]

#display
print(my_array)

#add the string element - "hello"
my_array.append("hello")

#display the list
print(my_array)

Output:

[1, 23, 45, 3, 34, 6, 7, 8, 9, 32]
[1, 23, 45, 3, 34, 6, 7, 8, 9, 32, 'hello']

Also ReadHow to Convert Binary to Decimal in Python [5 Methods]

2. clear()

This method is used to remove all the elements from the list.

Syntax:

my_array.clear()

Where my_array is an input list.

Example:

This example will create an array with 10 elements and remove all elements.

#create an  list
my_array=[1,23,45,3,34,6,7,8,9,32]

#display
print(my_array)

#clear the array
my_array.clear()

#display the list
print(my_array)

Output:

[1, 23, 45, 3, 34, 6, 7, 8, 9, 32]
[]

Also ReadHow to Reverse an Array In Python [Flip Array]

3. copy()

This method is used to copy the first list to the new list

Syntax:

my_array.copy()

Where my_array is an input list.

Example:

We will create an array with 10 elements and copy all elements to the first list in this example.

#create an  list
my_array=[1,23,45,3,34,6,7,8,9,32]

#display
print(my_array)

#copy the array
my_array1=my_array.copy()

#display the list
print(my_array1)

Output:

[1, 23, 45, 3, 34, 6, 7, 8, 9, 32]
[1, 23, 45, 3, 34, 6, 7, 8, 9, 32]

Also ReadPython For Loop Index With Examples

4. count()

This method is used to count the particular element in a list

Syntax:

my_array.count('element')

my_array is an input list, and the element is to be counted.

Example:

In this example, we will create an array with 10 elements and count particular elements in the list.

#create an  list
my_array=[1,1,45,45,34,6,7,7,9,45]

#display
print(my_array)

#get the count of 45
print(my_array.count(45))

#get the count of 1
print(my_array.count(1))

#get the count of 7
print(my_array.count(7))

Output:

[1, 1, 45, 45, 34, 6, 7, 7, 9, 45]
3
2
2

5. extend()

This method is used to add a list to a list.

Syntax:

my_array.append(list)

Where my_array is an input list, and the list is the other list to be appended to the first list.

Example:

In this example, we will create an array with 10 elements and add an exact list.

#create an  list
my_array=[1,23,45,3,34,6,7,8,9,32]

#display
print(my_array)

#append the list
my_array.extend(my_array)

#display the list
print(my_array)

Output:

[1, 23, 45, 3, 34, 6, 7, 8, 9, 32]
[1, 23, 45, 3, 34, 6, 7, 8, 9, 32, 1, 23, 45, 3, 34, 6, 7, 8, 9, 32]

6. index()

This method returns the index of a particular element in a list. Indexing starts with 0.

Syntax:

my_array.index('element')

Where my_array is an input list.

Example:

In this example, we will create an array with 10 elements and get the indices of particular elements in the list.

#create an  list
my_array=[1,1,45,45,34,6,7,7,9,45]

#display
print(my_array)

#get the index of 1
print(my_array.index(1))

#get the index of 45
print(my_array.index(45))

#get the index of 9
print(my_array.index(9))

Output:

[1, 1, 45, 45, 34, 6, 7, 7, 9, 45]
0
2
8

Also ReadHow to Exit Python Program [4 Methods]

7. insert()

This method is used to insert the elements at the given index position. It will take two parameters.

The first parameter is the index position where an element is to be inserted, and the second parameter takes the element.

Syntax:

my_array.insert(index,'element')

Where my_array is an input list.

Example:

This example will create an array with 10 elements and insert the elements at particular positions.

#create an  list
my_array=[1,1,45,45,34,6,7,7,9,45]

#display
print(my_array)

#insert the string - "hello" at third position
my_array.insert(2,"hello")

#insert the string - "welcome" at last  position
my_array.insert(10,"hello")

#insert the string - "bye" at fifth position
my_array.insert(4,"bye")

#display list
print(my_array)

Output:

[1, 1, 45, 45, 34, 6, 7, 7, 9, 45]
[1, 1, 'hello', 45, 'bye', 45, 34, 6, 7, 7, 9, 'hello', 45]

8. pop()

This method removes an element from the list based on the given index. By default, it will remove the last item. Indexing starts with 0.

Syntax:

my_array.pop(index)

Where my_array is an input list.

Example 1:

This example will create an array with 10 elements and apply pop() on the array.

#create an  list
my_array=[1,2,3,4,5,6,7,8,9,34]

#display
print(my_array)

#removes the last item
my_array.pop()

#display list
print(my_array)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 34]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Example 2:

In this example, we will create an array with 10 elements and apply pop() to remove elements present in the indices – 1,2, and 7.

#create an  list
my_array=[1,2,3,4,5,6,7,8,9,34]

#display
print(my_array)

#removes the element present at index - 1 
my_array.pop(1)

#removes the element present at index - 2
my_array.pop(2)

#removes the element present at index - 7
my_array.pop(7)


#display list
print(my_array)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 34]
[1, 3, 5, 6, 7, 8, 9]

9. remove()

This method is used to remove an element from the list.

Syntax:

my_array.pop(element)

Where my_array is an input list.

Example:

In this example, we will create an array with 10 elements and apply to remove() on the array to remove particular elements.

#create an  list
my_array=[1,2,3,4,5,6,7,8,9,34]

#display
print(my_array)

#removes the element 1
my_array.remove(1)

#removes the element 34
my_array.remove(34)

#removes the element 5
my_array.remove(5)


#display list
print(my_array)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 34]
[2, 3, 4, 6, 7, 8, 9]

Also ReadHow To Display A Calendar In Python

10. reverse()

This method is used to reverse the list of elements.

Syntax:

my_array.reverse()

Where my_array is an input list.

Example:

We will create an array with 10 elements and reverse the list in this example.

#create an  list
my_array=[1,2,3,4,5,6,7,8,9,34]

#display
print(my_array)

#reverse the array
my_array.reverse()

#display
print(my_array)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 34]
[34, 9, 8, 7, 6, 5, 4, 3, 2, 1]

11. sort()

This method is used to sort the list of elements.

Syntax:

my_array.sort(reverse)

Where my_array is an input list, and the reverse is a parameter that takes boolean values. It will sort the list in ascending order if set to False and in descending order if it is True.

Example 1:

In this example, we will create an array with 10 elements and sort the list in decreasing order.

#create an  list
my_array=[1,42,3,4,23,6,7,8,9,34]

#display
print(my_array)

#sort the array in descending order
my_array.sort(reverse=True)

#display
print(my_array)

Output:

[1, 42, 3, 4, 23, 6, 7, 8, 9, 34]
[42, 34, 23, 9, 8, 7, 6, 4, 3, 1]

Example 2:

In this example, we will create an array with 10 elements and sort the list in ascending order.

#create an  list
my_array=[1,42,3,4,23,6,7,8,9,34]

#display
print(my_array)

#sort the array in increasing order
my_array.sort(reverse=False)

#display
print(my_array)

Output:

[1, 42, 3, 4, 23, 6, 7, 8, 9, 34]
[1, 3, 4, 6, 7, 8, 9, 23, 34, 42]

Also ReadHow to Convert Binary to Decimal in Python [5 Methods]

12. len()

This function is used to get the total number of elements present in the list.

Syntax:

len(my_array)

Where my_array is an input list

Example:

In this example, we will get the length of the list.

#create an  list
my_array=[1,42,3,4,23,6,7,8,9,34]

#display
print(my_array)

#get the length
print(len(my_array))

Output:

[1, 42, 3, 4, 23, 6, 7, 8, 9, 34]
10

Summary

This tutorial taught us about creating empty arrays in Python and discussed adding, deleting, and sorting the elements in a list/array in Python.

To get the index from the list, we used the index() method, and also, to count the total number of occurrences of a particular element in a list, we used the count() method.

Finally, we closed this tutorial by seeing how to get the total number of elements in the list.

Himanshu Tyagi
Himanshu Tyagi
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator. With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!
RELATED ARTICLES