Multidimensional Array – Data Structure

If you already know about Array. Then learning Multidimensional Array will not difficult for you. If you want to read my article about Array, click here.

In short, when we write array inside an array, it’s become a multidimensional array. Let’s try to understand more clearly through examples.

Note: In python, we are going to use list to demonstrate array. Which is equivalent to an array.

vowels = [['A', 'E', 'I', 'O', 'U'], ['a', 'e', 'i', 'o', 'u']]
print(vowels)

# Retrieve value from a two-dimensional array
print(vowels[0][3]) # Output = O
print(vowels[1][3]) # Output = o

To print the value of array inside an array. First, we have to write the index of that array in the first square brackets. And In the second square brackets, we have to write the index of the element. See the above example.

In our first example, we see a multidimensional array, actually a two-dimensional array. First, we declare an array. Then two more inside that array. The first one contains all uppercase vowels. And the second one is lowercase.
Here the depth of the array inside the array is two. We can go further as we want. We can declare array inside an array even one more array inside that array and so on. See the next example.

random = [[1, ['One']], [2, ['Two']], [3, ['Three']], [4, ['four']], [5, ['Five']]]

# Print value
print(random[0]) # [1, ['One']]
print(random[1][0]) # 2
print(random[4][1][0]) # Five

# to delete the last element of the random array
del random[4]
# the last element deleted
print(random) # [[1, ['One']], [2, ['Two']], [3, ['Three']], [4, ['four']]]

In our previous example, the last element deleted from the random array with its two-child arrays.

We can do all operations in the multidimensional array like add an element, update, delete, etc.
I hope you get the minimal idea about Multidimensional Array.

Jagged Array

If we noticed previous examples, the length of child arrays is the same. A jagged array is nothing but an array of arrays with different lengths.

In computer science, a ragged array, also known as a jagged array, is an array of arrays of which the member arrays can be of different sizes and producing rows of jagged edges when visualized as output.

Wikipedia

Let’s try to understand more clearly through examples.

random = [[True, False], ['a', 'e', 'i', 'o', 'u'], None, [[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]]]
print(random) # [[True, False], ['a', 'e', 'i', 'o', 'u'], None, [[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]]]

# New Element
random.append('Python') 
print(random) #[[True, False], ['a', 'e', 'i', 'o', 'u'], None, [[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]], 'Python']

# Print Element 
print(random[-1]) # Python
print(random[0][0]) # True
print(random[3][1]) # [1, 3, 5, 7, 9]
print(random[3][1][0]) # 1

# Different Length
print(len(random[0])) # 2
print(len(random[3][0])) # 5

len() is a built-in Python function. It returns the length of the array (list in python).

Remember one thing, The arrays inside an array are but an element of that array. We should be careful about the index position when we access the value.


That’s all. I hope you get some overview of Multidimensional Array. I hope you liked this article. Thanks for reading.

Scroll to Top