Python Iterations

This article is about Iterations in Python.

What are iterations?

An iteration, in the context of programming, is a process wherein a set of instructions are repeated in a sequence for a specified number of times or until a certain condition is met. When the first set of instructions is executed again, it is called an iteration. 

Due to how common iterations are, Python provides all kinds of ways for us to implement iterations. Iterations most commonly go hand in hand with loops, like the for and while loops.

We’ll be demonstrating how to iterate over various kinds of data types like lists, arrays etc. A link to the individual data types will also be included.

Python Strings

Unlike most programming languages, Python treats its string as a sequence, where each character is a string of length 1. This makes it a collection of strings. This allows it to be iterated over.

a = "Hello"
for x in a:
     print(a)
H
e
l
l
o

Learn more about strings!

Python Lists

The following code will iterate over every single element in the list and print it out. The iteration is not effected by the type of the data in the list.

mylist = [1,2,3,4,5]
for x in mylist:
     print(x)
1
2
3
4
5

Learn more about Lists!

Python Tuples

The following code will iterate over every single element in the list and print it out. The iteration is not effected by the type of the data in the tuple.

mytuple = (5,8,3,2,4)
for x in mytuple:
     print(x)
5
8
3
2
4

Learn more about tuples!

Python Dictionaries

Python dictionaries comprise of a key : value pair. Thus, every element or “pair” in a dictionary has two values in it. However, when iterating over a dictionary, the value is ignored. The key is used to iterate.

mydict = { "Bob":16, "Sam": 21, "Alice": 15, "Naomi": 19}
for x in mydict:
    print(x)
Bob
Sam
Alice
Naomi

However, there is a simple way to print out all the values as well.

mydict = { "Bob":16, "Sam": 21, "Alice": 15, "Naomi": 19}
for x in mydict:
    print(x, mydict[x])
Bob 16
Sam 21
Alice 15
Naomi 19

Learn more about dictionaries!

Python Sets

The following code will iterate over every single element in the list and print it out. The iteration is not effected by the type of the data in the tuple.

myset = {5, 7, 4, "Cat", True}
for x in myset:
    print(x)
True
4
Cat
5
7

A set is a unordered collection of elements. Unlike lists, which would print out values respective to their location, the elements in the list print out randomly.

Python Arrays

We set up two for loops, as it’s a two dimensional array. The changing x and i variables allow us to go through each index, printing them out individually.

import numpy as np

arr2 = np.array([[1,2,3],
                [4,5,6]])

row_len = len(arr2)
col_len = len(arr2[0])
for x in range(row_len):
    for i in range(col_len):
        print(arr2[x][i])

First we found the lengths of the number of rows and columns in the array. Using the len() function on the array will only return number of rows. However, finding the len() of a row will return the number of elements in it, also known as the number of columns.

These two lengths are what we use in the for loops to define the end point.

Learn more about arrays!

range

While not actually a data type of it’s own, it counts as a sequence. Hence we’ll be demonstrating how iterations work in Python sequences. A similar function to this is the arange function from numpy.

rangeseq = range(1,11,2)
for x in rangeseq:
    print(x)
1
3
5
7
9

This marks the end of our Python Iterations article. Feel free to make any suggestions or contributions. Anything to help improve CodersLegacy is more than welcome.

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments