# Set a 1-D iterator over a Numpy array

NumpyServer Side ProgrammingProgramming

#### Python Data Science basics with Numpy, Pandas and Matplotlib

Most Popular

63 Lectures 6 hours

#### Data Analysis using NumPy and Pandas

19 Lectures 8 hours

#### Numpy with Python

Most Popular

12 Lectures 3 hours

For a 1-D iterator over the array, use the numpy.flat() method in Python Numpy. This is a numpy.flatiter instance, which acts similarly to, but is not a subclass of, Python’s built-in iterator object.

NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.

## Steps

At first, import the required library −

import numpy as np

Create a 2d array −

arr = np.array([[36, 36, 78, 88], [92, 81, 98, 45], [22, 67, 54, 69 ], [69, 80, 80, 99]])

Displaying our array −

print("Array...\n",arr)

Get the datatype −

print("\nArray datatype...\n",arr.dtype)


Get the dimensions of the Array −

print("\nArray Dimensions...\n",arr.ndim)

Get the shape of the Array −

print("\nOur Array Shape...\n",arr.shape)

Get the number of elements of the Array −

print("\nElements in the Array...\n",arr.size)

For a 1-D iterator over the array, use the numpy.flat() method in Python Numpy −

print("\nResult...\n",arr.flat)
print("\nResult...\n",arr.flat)
print("\nResult...\n",arr.flat)

Get the type of the flat iterator −

print("\nGet the type...\n",type(arr.flat))

## Example

import numpy as np

# Create a 2d array
arr = np.array([[36, 36, 78, 88], [92, 81, 98, 45], [22, 67, 54, 69], [69, 80, 80, 99]])

# Displaying our array
print("Array...\n",arr)

# Get the datatype
print("\nArray datatype...\n",arr.dtype)

# Get the dimensions of the Array
print("\nArray Dimensions...\n",arr.ndim)

# Get the shape of the Array
print("\nOur Array Shape...\n",arr.shape)

# Get the number of elements of the Array
print("\nElements in the Array...\n",arr.size)

# For a a 1-D iterator over the array, use the numpy.flat() method in Python Numpy
print("\nResult...\n",arr.flat)
print("\nResult...\n",arr.flat)
print("\nResult...\n",arr.flat)

# Get the type of the flat iterator
print("\nGet the type...\n",type(arr.flat))

## Output

Array...
[[36 36 78 88]
[92 81 98 45]
[22 67 54 69]
[69 80 80 99]]

Array datatype...
int64

Array Dimensions...
2

Our Array Shape...
(4, 4)

Elements in the Array...
16

Result...
78

Result...
45

Result...
67

Get the type...
<class 'numpy.flatiter'>