Cara menggunakan empty 2d array python

In this Python tutorial, We will discuss how built-in support works for the array in Python. Also, we can create an array by using a Python list with some examples.

  • How to create an empty array
  • How to create an empty 2D array in Python
  • How to reverse an array in python
  • Remove element from array python
  • How to sum an array in python
  • Check if an array is empty python

Table of Contents

Create empty array Python

In python, we don’t have built-in support for the array, but python lists can be used. Create a list [0] and multiply it by number and then we will get an empty array.

Example:

my_list = [0]*3
print(my_list)

After writing the above code (create empty array Python), Ones you will print ” my_list ” then the output will appear as “ [ 0, 0, 0 ] ”. Here, we created a list [0] and multiply it by ” x ” where ” x ” can be any number to get the length and every element will be ” 0 “.

You can refer to the below screenshot for Creating empty array in Python

Cara menggunakan empty 2d array python
Create empty array Python

This is how we can create empty array in python.

We can also create an empty array in python by list comprehension in which we will use for loop with range().

Example:

my_array = [0 for a in range(3)]
print(my_array)

After writing the above code (create empty array Python), Ones you will print ” my_array ” then the output will appear as “ [ 0, 0, 0 ] ”. Here, for loop iterates over a sequence and range() generate a sequence with the number and it creates an empty array.

You can refer to the below screenshot for Creating empty array in Python

Cara menggunakan empty 2d array python
Create an empty array in Python

This is how we can create empty array in python

Create empty 2D arrays in Python

In python, we can create 2D arrays in python by using a list. The 2D array is an array within an array. In 2D array, the position of the element is referred by two and it is represented by rows and columns.

Example:

my_rows, my_cols = (3, 4)
my_array = [[0]*my_cols]*my_rows
print(my_array)

After writing the above code (create an empty 2D array in Python), Ones you will print ” my_array ” then the output will appear as “ [ [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0] ] ”. Here, rows is 3 and column is 4 and the list is [0] and it gets multiplied with the rows and columns.

You can refer to the below screenshot for creating empty 2D array in Python

Cara menggunakan empty 2d array python
Create empty 2D arrays in Python

This is how we can create empty 2D arrays in python.

How to reverse an array in python

In python, to reverse an array in python we have the built-in method called reverse() which will reverse the items.

Example:

my_array = [10, 11, 12, 13]
my_array.reverse()
print('After reversing my array:', my_array)

After writing the above code (how to reverse an array in python), Ones you will print ” my_array ” then the output will appear as “ [ 13, 12, 11, 10 ] ”. Here, reverse() method will reverse the order of the items.

You can refer to the below screenshot how to reverse an array in python

Cara menggunakan empty 2d array python
How to reverse an array in python

This is how to reverse an array in python

Remove element from array python

In python, to remove an element from the array we can use the pop() method to remove the particular elements from the list python.

Example:

my_array = [101, 102, 103]
my_array.pop(2)
print(my_array)

After writing the above code (remove an element from array python), Ones you will print ” my_array ” then the output will appear as “ [ 101, 102 ] ”. Here, the pop() method will remove the specified element from an array.

You can refer to the below screenshot how to remove element from array python

Cara menggunakan empty 2d array python
Remove element from array python

This is how we can remove element from array python

How to sum an array in python

In python, to sum an elements in an array we will use for loop through an array and it will add the value of elements in each iteration.

Example:

my_array = [150, 151, 152]
sum = 0;
for a in range(0, len(my_array)):
sum = sum + my_array[a];
print("Sum of element in an array: " + str(sum));

After writing the above code (how to sum an array in python), Ones you will print ” sum ” then the output will appear as “ Sum of elements in an array: 453 ”. Here, we will use for loop and it will add all the elements in each iteration.

You can refer to the below screenshot how to sum an array in python

Cara menggunakan empty 2d array python
How to sum an array in python

This is how to sum an array in python

Check if an array is empty python

In python, to check if an array is empty or not we will use the if condition to check whether the array is empty or not.

Example:

my_array = []
if not my_array:
print("My array is empty")

After writing the above code (check if an array is an empty python), Ones you will print then the output will appear as “ My array is empty ”. Here, we will use the if condition to check whether my array is empty or not.

You can refer to the below screenshot check if an array is empty python.

Cara menggunakan empty 2d array python
Check if an array is empty python

This is how we can check if an array is empty python

You may like the following Python tutorials:

  • Invalid syntax in python
  • syntaxerror invalid character in identifier python3
  • Python Addition Examples
  • Multiply in Python with Examples
  • How to handle indexerror: string index out of range in Python
  • Unexpected EOF while parsing Python

In this tutorial, we learned how to work with array in python.

  • How to create an empty array
  • How to create an empty 2D array in Python
  • How to reverse an array in python
  • Remove element from array python
  • How to sum an array in python
  • Check if an array is empty python

Cara menggunakan empty 2d array python

Bijay Kumar

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.