# Iteration in Python – enumerate(), item(), np.nditer(), iterrows()

Anchit Bhagat 28 Sep, 2022 â€¢ 2 min read

This article was published as a part of the Data Science Blogathon.

## Introduction

An iteration is an object that repeats identical or similar tasks without making errors. In a way, we can say repeated execution of a set of statements is what iteration is all about. Python has several language features to make it easier to perform the iteration task.

As an object, the iterator counts a number of values that can be iterated upon. Lists, tuples, dictionaries, strings, and sets are all iterable objects. They are iterable containers from which you can get an iterator from.

In the following topic, weâ€™ll see a brief of different processes of iteration.

Â

## Looping using enumerate()

Using a for loop to iterate over a list only gives us access to every list element in each run, one after the other. If one also wants to access the index information, so where the list element we are iterating over is located, we can use enumerate().

As an example, have a look at how the for loop was converted by creating an areas list:
Python Code:

## Loop over Numpy array – np.nditer()

If we’re dealing with a 1D Numpy array, looping over all elements can be as simple as:

`for x in my_array :`

If weâ€™re dealing with a 2D Numpy array, it’s more complicated. A 2D array is built up of multiple 1D arrays. To explicitly iterate over all separate elements of a multi-dimensional array, we’ll need this syntax:

`for x in np.nditer(my_array) :`

Below we are writing a for loop that iterates over all elements in np_height and prints out “x inches” for each element, where x is the value in the array.

```# Import numpy as np
import numpy as np

# For loop over np_height
for x in np_height:
print(x, "inches")

# For loop over np_baseball
for n in np.nditer(np_baseball):
print(n)```

#### Output:

```74 inches
74 inches
72 inches
72 inches
73 inches
69 inches
69 inches
71 inches
76 inches
71 inches
73 inchesâ€¦..```

## Looping through iterrows():

Using iterrows() to iterate over every observation of a Pandas DataFrame. Here, we are using a for loop to add a new column, named COUNTRY, that contains an uppercase version of the country names in the “country” column. We are using the string method upper()Â for this.

```# Import cars data
import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)

# Code for loop that adds COUNTRY column
for lab, row in cars.iterrows():
cars.loc[lab, "COUNTRY"] = row['country'].upper()

# Print cars
print(cars)```

Using iterrows() to iterate over every observation of a Pandas DataFrame is easy to understand, but not very efficient. On every iteration, we are creating a new Pandas Series in Python. If we want to add a column to a DataFrame by calling a function on another column, the iterrows() method in combination with a for loop is not the preferred way to go. Instead, we’ll want to use apply()

Below weâ€™ll use the apply()Â version to get the same result in the DataFrame:

```# Use .apply(str.upper)
cars["COUNTRY"] = cars["country"].apply(str.upper)
print(cars)```

#### Output:

```cars_per_cap country drives_right (US, COUNTRY)
US 809 United States True UNITED STATES
AUS 731 Australia False AUSTRALIA
JPN 588 Japan False JAPAN
IN 18 India False INDIA
RU 200 Russia True RUSSIA
MOR 70 Morocco True MOR```

We can utilize the above itertools to work with the iteration in python in a more effective way. This was just the brief on iteration. One can work over it with different examples.

The media shown in this article are not owned by Analytics Vidhya and is used at the Authorâ€™s discretion.

Anchit Bhagat 28 Sep 2022