Introduction to 3D Arrays in Python
Before starting with 3d array, one thing to be clear that arrays are in every programming language is there and does some work in python also. Every programming language its behavior as it is written in its compiler. Many people have one question: Do we need to use a list in the form of 3d array, or we have Numpy. And the answer is we can go with the simple implementation of 3d arrays with the list. But for some complex structure, we have an easy way of doing it by including Numpy. It is not recommended which way to use it. It depends on the project and requirement that how you want to implement particular functionality.
What does the library mean?
Python has a set of libraries defines to ease the task. For the same reason to work with array efficiently and by looking at today’s requirement, Python has a library called Numpy. Numpy deals with the arrays. Numpy is useful in Machine learning also. It is good to be included as we come across multi-dimensional arrays in python. As we know, arrays are to store homogeneous data items in a single variable. Arrays in Python is nothing but the list. Look at the following code snippet. Here, we have a list of named colors. We are printing colors. This is a simple single-dimensional list we can say.
colors = ["red", "blue", "orange"] print(colors)
Output:[‘red’, ‘blue’, ‘orange’]
Also, multidimensional arrays or a list have row and column to define. We can say that multidimensional arrays as a set of lists.
Following is the example of 2 dimensional Array or a list.
rows = int(input("Enter the no.of rows you want: ")) cols = int(input("Enter the number of cols you want: ")) myList = [[0 for c in range(cols)] for r in range(rows)] for r in range(rows): for c in range(cols): myList[r][c]= r*c print(myList)
Enter the no. of rows you want: 2
Enter the number of cols you want: 2
[[0, 0], [0, 1]]
In the above example, we are just taking input from the end-user for no. of rows and columns. After that, we are storing respective values in a variable called rows and cols. Further, we created a nested loop and assigned it to a variable called my list. Here we are just taking items to be a loop over the numbers, which we are taking from end-user in the form of rows and cols.
After that, we are a loop over rows and columns. Finally, we are generating the list as per the numbers provided by the end-user.
Try this program. If you don’t know about how for loop works in python, then first check that concept and then come back here. You will understand this better.
How it is defined in Python?
Suppose we have a matrix of 1*3*3. We need to define it in the form of the list then it would be 3 items, 3 rows, and 3 columns.
In the above diagram, we have only one @ in each set, i.e. one element in each set. 3 columns and 3 rows, respectively.
How can we define it then? In python, with the help of a list, we can define this 3-dimensional array. 3-dimensional arrays are arrays of arrays. There is no limit while nesting this.
How to Create 3D Arrays in Python?
We are creating a list that will be nested. Try out the following small example. If you are familiar with python for loops, then you will easily understand the below example.
symbol = [[ ['@' for col in range(2)] for col in range(2)] for row in range(3)] print(symbol)
Output:[[[‘@’, ‘@’], [‘@’, ‘@’]], [[‘@’, ‘@’], [‘@’, ‘@’]], [[‘@’, ‘@’], [‘@’, ‘@’]]]
If you look closely at the above example, we have one variable of type list. With the square brackets, we are defining a list in python. In the list, we have given for loop with the help of the range function. Which is simply defines 2 elements in one set. Each sublist will have two such sets. And we have a total of 3 elements on the list.
How to Insert Elements of 3D Arrays in Python?
Python has given us every solution that we might require. Python has many methods predefined in it. These methods help us to add an element to a given list. Python does not support the array fully. At this point, to get simpler with the array, we need to make use of function insert.
Kindly look at the below program.
mylist = [[['@', '@'], ['@', '@']], [['@', '@'], ['@', '@']], [['@', '@'], ['@', '@']]] # number tuple addition = ['$','$'] # inserting $ symbol in the existing list my list.insert(2, addition) print('Updated List is: ', mylist)
Updated List is: [[[‘@’, ‘@’], [‘@’, ‘@’]], [[‘@’, ‘@’], [‘@’, ‘@’]], [‘$’, ‘$’], [[‘@’, ‘@’], [‘@’, ‘@’]]]
Here, in the above program, we are inserting a new array element with the insert method’s help, which python provides. In the above program, we have one 3 dimensional lists called my list.
The insert method takes two arguments. One is position, i.e. nothing but the index number. And second is an actual element you want to insert in the existing array or a list. Here, we took the element in one variable which we wanted to insert. We are applying the insert method on mylist.
Try to execute this program. Play with the output for different combinations. In the above program, we have given the position as 2. We all know that the array index starts at zero (0). That means a new element got added into the 3rd place, as you can see in the output.
How to Remove Elements of 3D Arrays in Python?
If we want to remove the last element in a list/array, we use a pop method. Look at the below example. Here we have removed the last element in an array. We have a pop() method. This method removes the last element in the list. We have used a pop() method in our 3d list/array, and it gives us a result with only two list elements. Try out the following example.
symbol = [[ ['@' for col in range(2)] for col in range(2)] for row in range(3)] symbol.pop() print(symbol)
Output:[[[‘@’, ‘@’], [‘@’, ‘@’]], [[‘@’, ‘@’], [‘@’, ‘@’]]]
Here, we will look at the Numpy. As we already know, Numpy is a python package used to deal with arrays in python. Let’s start to understand how it works. For using this package, we need to install it first on our machine. For installing it on MAC or Linux, use the following command.
Pip Install Numpy
Let’s discuss how to install pip in NumPy.
- Forgetting it on windows, we need to install it by an installer of Numpy. We are not getting in too much because every program we will run with numpy needs a Numpy in our system.
- Numpy has a predefined function which makes it easy to manipulate the array. An array is generally like what comes with a fixed size. Increasing or decreasing the size of an array is quite crucial. Numpy overcomes this issue and provides you with good functionality to deal with this.
- To start work with Numpy after installing it successfully on your machine, we need to import it into our program. After importing, we are using an object of it.
- Using Numpy has a set of some new buzzword as every package has. If you want to learn more about Numpy, then do visit the link: https://docs.scipy.org/doc/ .
- Here you will find the most accurate data and the current updated version of Numpy.
Python is a scripting language and mostly used for writing small automated scripts. If we closely look at the requirements that we should know, then it is how to play with multi-dimensional arrays. With Python, we can write a big script with less code. Many emerging technologies need this aspect to work. ML, AI, big data, Hadoop, automation needs python to do more in fewer amounts of time. The packages like Numpy will be the added advantage in this.
This is a guide to 3d Arrays in Python. Here we discuss how 3D Arrays are defined in Python along with creation, insertion and removing the elements of 3D Arrays in Python. You may also look at the following articles to learn more –