# How to Create an Empty Array In Python

0
533 This post was last Updated on June 7, 2022 by Himanshu Tyagi to reflect the accuracy and up-to-date information on the page.

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 that will store 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:

`[]`

## Method 3: Creating an empty list array with 0’s

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

```#create an  empty list
my_array=*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: Creating 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:

We will create an array with 10 elements and add a string value to the list in this example.

```#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']```

### 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]
[]```

### 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')`

where my_array is an input list and 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 to the 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]```

Also ReadHow to Convert Python Tuples to Lists

### 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]```

Also ReadHow To Sort a List of Tuples in Python

### 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 it is set to False and sorts the list in descending order if it is True.

Example 1:

We will create an array with 10 elements in this example 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:

We will create an array with 10 elements in this example 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]```

### 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

From this tutorial, we learned about creating empty arrays in Python, and we discussed how to add, delete and sort 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 present in the list.