- Print Array in Python
- Print List
- Using print()
- Using map()
- By unpacking list
- Using loop
- Print Numpy-Array
- Using print()
- Using loop
- Was this post helpful?
- You may also like:
- Bash Get Output from Python Script
- Count Unique Values in NumPy Array
- Create Array of Arrays in Python
- Convert String List to Integer List in Python
- Convert Object to Float in Pandas
- NameError: Name requests Is Not Defined
- Python Hex to String
- NameError: Name xrange Is Not Defined in Python
- Call Python Script from Bash with Arguments
- TypeError: ‘dict_values’ Object Is Not Subscriptable
- Share this
- Related Posts
- Author
- Related Posts
- Count Unique Values in NumPy Array
- Create Array of Arrays in Python
- Create Empty Array in Python
- Create Array of All NaN Values in Python
- Convert String Array to Int Array in Python
- Fill Array with Random Numbers in Python
- How to Print an Array in Python
- Methods To Print an Array in Python
- Printing Directly With The print() Method
- Using For Loop In Python
- How To Print NumPy Array In Python
- Using Simple Print Method
- Using For Loop
- Wrap Up
- How to Print an Array in Python
- Printing an array using for loop
- Printing a two-dimensional array in Python
- Printing a multidimensional array
- Printing a list
- Leave a Comment Cancel reply
Print Array in Python
In this post, we will see how to print array in Python.
As we know that, Python didn’t have an in-built array data type , so we try to use list data type as an array. We can also use the NumPy module for creating NumPy array and apply array operation on it.
Now, we will first see how to print the list as an array, and then we will see how to print NumPy array in Python.
Print List
Using print()
Here, print() function is used to print the whole array along with [] .
Below is the Python code given:
As you can see here, arr is one dimension array and you just need to pass it to print() method.
Similiarly, arr2d is two dimensional array and represents list of lists. You need to pass it to print() method to print 2D array in Python.
Using map()
If you directly use print() method to print the elements, then it will printed with [] .
In case, you want to print elements in desired way, you can use map() method with join() method.
map() method will convert each item to string and then use join() method to join the strings with delimeter.
Below is the Python code given:
By unpacking list
You can use *list to print list of elements without brackets in Python 3.X .
*list simply unpacks the list and pass it to print function.
Below is the Python code given:
If you are using Python 2.x then you can import print function as below:
Using loop
Here, for loop is used to iterate through every element of an array, then print that element. We can also use while loop in place of for loop.
Below is the Python code given:
Here, we traversed to one dimenstional array arr and two dimensional array arr2d and print its elements.
Print Numpy-Array
Using print()
Here, print() function is used to print the whole NumPy array along with [].
Below is the Python code given:
Here arr and arr2d are one dimensional numpy array and two dimensional numpy array respectively. You need to pass it to print() method to print the array.
Using loop
Here, for loop is used to iterate through every element of a NumPy-array then print that element. We can also use while loop in place of for loop.
Below is the Python code given:
That’s all about how to print Array in Python.
Was this post helpful?
You may also like:
Bash Get Output from Python Script
Count Unique Values in NumPy Array
Create Array of Arrays in Python
Convert String List to Integer List in Python
Convert Object to Float in Pandas
NameError: Name requests Is Not Defined
Python Hex to String
NameError: Name xrange Is Not Defined in Python
Call Python Script from Bash with Arguments
TypeError: ‘dict_values’ Object Is Not Subscriptable
Share this
Related Posts
Author
Related Posts
Count Unique Values in NumPy Array
Table of ContentsUse np.unique() method to display unique valuesUse np.unique() method with len() functionUse np.unique() Method with return_counts as true Use np.unique() method to display unique values Use np.unique() to display unique values in numpy array. [crayon-64c3d12529157585584820/] Running the above code will display the following output on the console: [crayon-64c3d12529160943014057/] np.unique() method finds unique values […]
Create Array of Arrays in Python
Table of ContentsUse numpy.array() FunctionManually create array of arraysUse numpy.append() Function Use numpy.array() Function To create an array of the arrays in Python: Use the np.array() function to create a numpy.ndarray type array of the arrays. [crayon-64c3d1252937f422207529/] [crayon-64c3d12529385195700586/] The Python library NumPy scientifically computes advanced numerical work. It is a language extension that adds support […]
Create Empty Array in Python
Table of ContentsCreating an Empty Array in PythonUse the numpy Library to Create an Empty ArrayUse List Comprehension to Create an Empty ArrayUse the array Module to Create an Empty Array Creating an Empty Array in Python Before moving towards various solutions for creating an empty array in Python, we must understand what an empty […]
Create Array of All NaN Values in Python
Table of ContentsUsing numpy.empty() FunctionUsing numpy.full() FunctionUsing numpy.tile() FunctionUsing numpy.repeat() FunctionUsing Multiplication of numpy.ones() with nan Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-64c3d1252a11d853019546/] [crayon-64c3d1252a123825417528/] We used numpy.empty() […]
Convert String Array to Int Array in Python
Table of ContentsUsing the for loop with int() functionUsing for loop with eval() functionUsing the map() with list() functionConclusion This tutorial will demonstrate how to convert string array to int array in Python. Using the for loop with int() function To convert string array to int array in Python: Use the for loop to loop […]
Fill Array with Random Numbers in Python
Table of ContentsFill array with random numbers in PythonWays to fill array with random numbers in PythonUsing the numpy.random.randint() function to fill array with random numbers in PythonUsing the numpy.random.Generator.integers() function to fill array with random numbers in PythonUsing the random.randint() function to fill array with random numbers in PythonConclusion Fill array with random numbers […]
How to Print an Array in Python
In this tutorial, you will learn how to print an array in Python.
Arrays are collections of data elements of the same type with the same name. Arrays can be implemented in Python using lists or the NumPy module. The NumPy module provides us with arrays of type ndarray (NumPy Array).
An array can also be multi-dimensional. Two-dimensional arrays are, as we all know, the most basic form of multi-dimensional arrays. As a result, in this tutorial, we will look at both 1D and 2D Arrays.
Methods To Print an Array in Python
Let’s take a look at some of the methods for printing both 1D and 2D arrays in Python. It should be noted that these arrays will be implemented using lists.
Printing Directly With The print() Method
To print the values from an array(list), we can directly pass the name of the array(list) to Python’s print() method.
However, in this case, the array is printed as a list, with brackets and values separated by commas.
#Initializing the Array array1D = [1,2,3] array2D = [[4,5,6], [7,8,9]] #Printing The Array Directly Using Print print(array1D) print(array2D)
array1D is a one-dimensional array in this case. array2D, on the other hand, is a two-dimensional one. We directly pass their names to the print() method, which prints them as a list and a list of lists, respectively.
Using For Loop In Python
In Python, we can also print an array by traversing all of its elements with for loops. Let us see in the below example code.
#Initializing the Array array1D = [1,2,3] array2D = [[4,5,6], [7,8,9]] #Printing The Array Directly Using Print print("Printing 1D Array Using For Loop: ") for item in array1D: print(item, end=' ') print("\nPrinting 2D Array Using For Loop:") for givenList in array2D: for item in givenList: print(item, end=' ')
Printing 1D Array Using For Loop: 1 2 3 Printing 2D Array Using For Loop: 4 5 6 7 8 9
Using for loops, we traverse the elements of a 1D and a 2D Array and print the corresponding elements in our desired form in the code above.
How To Print NumPy Array In Python
As previously stated, arrays can also be implemented in Python using the NumPy module. The module includes a pre-defined array class that can store values of the same type.
These NumPy arrays can be multidimensional as well. So, let’s see how we can print both 1D and 2D NumPy arrays in Python.
Using Simple Print Method
To print the arrays, we can directly pass the NumPy array name to the print() method, as we did with arrays implemented using lists.
#importing the NumPy Library import numpy as np #Initializing NumPy Array array2D = np.array([[5,6],[7,8,9]]) array1D = np.array([1,2,3,4]) #printing the 1d numpy array print("Numpy 1D array is: ", array1D) #printing the 2d numpy array print("Numpy 2D-array is: ", array2D)
Numpy 1D array is: [1 2 3 4] Numpy 2D-array is: [list([5, 6]) list([7, 8, 9])]
An array1D and array2D are NumPy arrays of 1D and 2D dimensions, respectively. We pass their names to the print() method, which prints them both. It’s worth noting that the arrays are still printed in the form of NumPy arrays with brackets this time.
Using For Loop
Again, we can use loop structures to traverse NumPy arrays in Python. As a result, we can access and print each element of the array. This is yet another method for printing an array in Python.
Take a close look at the example below.
#importing the NumPy Library import numpy as np #Initializing NumPy Array array2D = np.array([[5,6],[7,8,9]]) array1D = np.array([1,2,3,4]) #printing the 1d numpy array print("Numpy 1D array is: ") for item in array1D: print(item, end=' ') #printing the 2d numpy array print("\nNumpy 2D-array is: ") for preList in array2D: for item in preList: print(item, end=' ')
Numpy 1D array is: 1 2 3 4 Numpy 2D-array is: 5 6 7 8 9
We print the NumPy array elements in the desired manner (without brackets) here as well by accessing the elements of the 1D and 2D arrays individually.
Wrap Up
So we learned how to print an array in Python in this tutorial. I hope you now have a firm grasp on the subject. Please use the comments section if you have any further questions about the topic.
If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.
Further Read:
How to Print an Array in Python
To print an array in Python, you can use the “print()” function. The print() function takes the name of the array containing the values and prints it.
import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr)
We created a ndarray object using the np.array() function. The print() function takes an arr as an argument and prints the array directly to the console. The printed array is a One-dimensional array because we created a one-dimensional array.
Printing an array using for loop
To print the elements of the array line by line or with a custom format, you can use the for loop.
import numpy as np # Create a NumPy array array = np.array([1, 2, 3, 4, 5]) # Print the elements line by line for element in array: print(element)
Printing a two-dimensional array in Python
To print a two-dimensional array in Python, you can use the print() function.
import numpy as np arr = np.array([[1, 2], [3, 4], [5, 6]]) print(arr)
And we printed the 2D Python array using the print() function.
Printing a multidimensional array
If you are working with a multidimensional array, you can use nested for loops to print the elements.
import numpy as np # Create a 2D NumPy array array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Print the elements line by line for row in array: for element in row: print(element, end=" ") print()
Printing a list
To print the list in Python, you can use the print() function. Python does not have a built-in Array data type, but it has a list you can consider an array in some cases.
list = [11, 19, 21, 46] print(list)
And that’s how you print the list and array in Python.
That’s it for this tutorial.
Leave a Comment Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.