3 Practical Python 3 For Loop Examples

Table of Contents

A Python for loop is a set of instructions that is repeated, or iterated, for every value in a sequence.

Sometimes for-loops are referred to as definite loops because they have a predefined beginning and end as bounded by the sequence.

Python For loop Syntax

The general syntax of a for-loop block is as follows.

for looping variable in sequence:
……code block

  • A for-loop assigns the looping variable to the first element of the sequence. It executes everything in the code block.
  • Then it assigns the looping variable to the next element of the sequence and executes the code block again.
  • It continues until there are no more elements in the sequence to assign.

Python For Loops Over a List

We can use it with any sequence type (list, string, tuple) plus dictionaries. The most prototypical use of for … in loop is over a list.

Below, it is used to print all the items in the list:

>>> list=[‘Accelerate’, ‘your’, ‘transformation’, ‘with’, ‘Google’, ‘Cloud’]
>>> for i in list:
>>>… print(i)


Python For Loops Over a String

When used on a string, the looping has to go through everything. Well, a string is a sequence of characters, so the iteration is done on every character:

>>> for x in ‘google’:


Python For Loops Over a Dictionary

we can also use for loop over a dictionary. When for loop in is used on a dictionary, looping is done over its keys, and not over the values or the key:value pairs:

>>> Clouds = {‘Goole’:100, ‘Amazon’:20, ‘IBM’:10, ‘Cisco’:8}
>>> for s in Clouds: # s iterates over keys in clouds


Of course, the values are retrievable via the keys. Hence, printing both the key and the value looks like:

>>> for s in Clouds: # s iterates over keys in clouds
>>> ……print(s, Clouds[s]) # key, value

Goole 100
Amazon 20
IBM 10
Cisco 8

An alternative is to explicitly instruct the for loop to iterate over the key:value pairs.

The .items() method on a dictionary induces a pseudo-list of (key, value) tuples.

The for loop then can iterate over this list, and the bound variable should also be the tuple type:

>>> Clouds.items() # returns a quasi-list of (key, value) pairs
dict_items([(‘Goole’, 100), (‘Amazon’, 20), (‘IBM’, 10), (‘Cisco’, 8)])
>>> for (k,v) in Clouds.items(): # iterate over (key, value) list
>>> ……print(k, v) # key, value

Goole 100
Amazon 20
IBM 10
Cisco 8

Note that the iteration is in no particular order, although it stays consistent. That’s because dictionaries are inherently orderless.

If we want to go through the dictionary in a particular order (say, alphabetical or numerical), we will have to sort.

Python For loop With range() Function

range() is a function that’s often used with a for loop. range(x,y) creates a list-like object starting with integer x and ending BEFORE y.

The starting point x can be omitted, in which case the list starts with 0. In a looping environment range() works as expected.

In a non-iterative environment, however, we will need to plug in the range() function’s return value into list() to view it as a list:

>>> for i in range(4): # range() in iterative environment
>>> ……print(i)
>>> range(4) # non-iterative context: content not visible
>>> list(range(4)) # force into list type to see what’s inside
[0, 1, 2, 3]
>>> list(range(2,8))
[2, 3, 4, 5, 6, 7]

Below is another example of for loop with length.

The reason why range() is used often with a for loop is the following.

The way the loop is written, i is bound to the character itself, and there’s nothing that references the location of an individual character. range() solves this.

First, range() together with len(‘Google’) produces a list of indexes for the word:

>>> len(‘Google’)
>>> list(range(len(‘Google’)))
[0, 1, 2, 3, 4, 5]
Now, we can iterate through this list of indexes, and it is easy enough to get to the character once you have its index:

>>> for i in range(len(‘Google’)):
>>> ……print(i, ‘Google'[i])

0 G
1 o
2 o
3 g
4 l
5 e

Table of Contents

Share on facebook
Share on twitter
Share on linkedin

You might also like