Python: for loop

This blog presents for loop for multiple data structures, like loop over a range, loop over a string, loop over a numpy array, loop over a list, loop over a dictionary, loop over a series and loop ...

for loops are traditionally used when you have a block of code which you want to repeat a fixed number of times. The Python for statement iterates over the members of a sequence in order, executing the block each time. In this blog, except for for loop’s syntax and flow diagram, I’ll also talk about how to achieve loops over:

  • a range
  • a string
  • a numpy array
  • a list
  • a list of lists
  • a dictionary
  • a series
  • a dataframe

Syntax

for iterating_var in sequence:
    statements(s)

Flow diagram

If a sequence contains an expression list, it is evaluated first. Then, the first item in the sequence is assigned to the iterating variable iterating_var. Next, the statements block is executed. Each item in the list is assigned to iterating_var, and the statement(s) block is executed until the entire sequence is exhausted.

Loop of a range

>>> for x in range(0, 7):
...     print(x)
...
0
1
2
3
4
5
6

Loop of a string

>>> for x in 'Hi, Python':
...     print(x)
...
H
i
,

P
y
t
h
o
n

Loop of a numpy array

>>> import numpy as np
>>> np_height = np.array([3, 21, 4, 54, 23])
>>> for height in np_height:
...     print(height)
...
3
21
4
54
23

The iterator object nditer provides many flexible ways to visit all the elements of one or more arrays in a systematic fashion.

>>> for height in np.nditer(np_height):
...     print(height)
...
3
21
4
54
23

Loop of a list

>>> areas = [11.25, 18.0, 20.0, 10.75, 9.50]
>>> for area in areas:
...     print(area)
...
11.25
18.0
20.0
10.75
9.5

enumerate is a built-in function of Python. It allows us to loop over something and have an automatic counter.

>>> for counter, value in enumerate(areas):
...     print(counter, value)
...
0 11.25
1 18.0
2 20.0
3 10.75
4 9.5

Loop of a list of lists

>>> house = [["hallway", 11.25],
...          ["kitchen", 18.0],
...          ["living room", 20.0],
...          ["bedroom", 10.75],
...          ["bathroom", 9.50]]
>>> for room, area in house:
...     print('The ' + str(room) + ' is ' + str(area) + ' sqm.')
...
The hallway is 11.25 sqm.
The kitchen is 18.0 sqm.
The living room is 20.0 sqm.
The bedroom is 10.75 sqm.
The bathroom is 9.5 sqm.

Loop of a dictionary

>>> europe = {'Spain': 'Madrid',
...           'France': 'Paris',
...           'Germany': 'Berlin',
...           'Norway': 'Oslo',
...           'Italy': 'Rome',
...           'Poland': 'Warsaw',
...           'Australia': 'Canberra'}
>>> for key, value in europe.items():
...     print('The capital of ' + str(key) + ' is ' + str(value) + '.')
...
The capital of Spain is Madrid.
The capital of France is Paris.
The capital of Germany is Berlin.
The capital of Norway is Oslo.
The capital of Italy is Rome.
The capital of Poland is Warsaw.
The capital of Australia is Canberra.

Remark: dict.items() returns iterator object. dict.iteritems() is removed in python3.

Loop of a series

>>> import pandas as pd
>>> ser = pd.Series([4, -7, 2, 1])
>>> for v in ser:
...     print(v)
...
4
-7
2
1

Both pandas.Series.items and pandas.Series.iteritems lazily iterate over (index, value) tuples.

>>> for i, v in ser.items():
...     print(i, v)
...
0 4
1 -7
2 2
3 1
>>> for i, v in ser.iteritems():
...     print(i, v)
...
0 4
1 -7
2 2
3 1

Loop of a dataframe

>>> df = pd.DataFrame({'state': ['Ohio', 'Ohio', 'Nevada'],
...                    'year': [2000, 2001, 2001],
...                    'pop': [1.5, 1.7, 3.6]})
>>> for i, v in df.iterrows():
...     print(i, v)
...
0 state    Ohio
year     2000
pop       1.5
Name: 0, dtype: object
1 state    Ohio
year     2001
pop       1.7
Name: 1, dtype: object
2 state    Nevada
year       2001
pop         3.6
Name: 2, dtype: object

pandas.DataFrame.iterrows iterates over dataFrame rows as (index, Series) pairs.

Conclusion

In this blog, we talked about syntax of for loop, and its applications: loop over a range, loop over a string, loop over a numpy array, loop over a list, loop over a dictionary, loop over a series and loop over a dataframe. Hope it’s useful for you :)

Reference