Python for
Jakob Jenkov |
The Python for loop instruction is used to loop through (iterate) the elements of a list, tuple, set or range. Looping through the elements of a given collection or range is a commonly needed functionality, so the Python for instruction is very useful.
Python Data Types Supporting Iteration
The following data types in Python support that you iterate their elements:
- Range
- List
- Tuple
- Set
- Dictionary
- String
Iterating each of these data types will be covered in the following sections.
Python for Loop with Range
Here is an example of a Python for loop that loops through the elements of a number range:
for i in range(3): print("i is", i)
This loop will iterate the numbers in the range (from 0 to, but excluding, 3) and for each iteration assign the value of the next number in the sequence to the variable i. The body of the for loop instruction is repeated once for each element in the iterated sequence. In this case the for loop body is repeated 3 times.
Notice how the body of the for loop instruction is indented using exactly 4 spaces compared to the indentation level of the for loop instruction itself. This 4 space indentation is mandatory in Python.
The output printed from the above code example would be:
i is 0 i is 1 i is 2
A range in Python extends from a start value to an end value. The start value is included in the sequence, but the end value is not included in the sequence. Thus, a range that runs from 0 to 3 actually only contains the numbers 0, 1 and 2 .
Python for Loop with List
You can also iterate the elements of a Python list using a Python for loop. Here is an example of iterating the elements of a Python list using a Python for loop:
mylist = ["A", "B", "C"] for e in mylist: print("e is", e)
The output printed from the above Python loop example would be:
e is A e is B e is C
Python for Loop with Tuple
Iterating a Python tuple using a Python for loop looks similar to iterating a Python list. Here is an example of iterating a Python tuple using a Python for loop:
mytuple = ("X", "Y", "Z") for e in mytuple: print("e is", e)
The output of the above Python loop example would be:
e is X e is Y e is Z
Python for Loop with Set
You can also iterate the elements of a Python set using the Python for loop instruction. Here is how iterating a Python set using a Python for loop looks:
myset = set(["one", "two", "three"]) for e in myset: print("e is", e)
The output from the above Python for loop example would be similar to this:
e is three e is two e is one
Since a Python set has no guaranteed intrinsic order of its elements, you have no guarantee about
the sequence in which you iterate them using a Python for loop. In the example above, the order
was the reverse of the order of the list passed to the set()
function as parameter.
Python for Loop with Dictionary
You can also iterate the keys of a Python dictionary using a Python for loop. Here is how iterating the keys of a Python dictionary looks using a Python for loop:
mydictionary = dict() mydictionary['a'] = "First" mydictionary['b'] = "Second" mydictionary['c'] = "Third" for key in mydictionary: print(key, "=", mydictionary[key])
The output from the above Python for loop example would be similar to this:
a = First b = Second c = Third
Note: You have no guarantee about the order the keys of a Python dictionary are stored internally, so there is no guarantee about the order in which they are iterated with a Python for loop.
Python for Loop with String
It is also possible to use a Python for loop with a Python string. Here is an example of iterating all characters in a Python string using a Python for loop:
mystring = "AXBYCZ" for char in mystring: print(char)
For each iteration in the for loop the next character in the mystring string will be assigned to the char variable. Inside the body of the for loop the value of the char variable is printed. The output printed from the above Python for loop example would be similar to this:
A X B Y C Z
for Loops in Other Languages
For the curious reader, here are links to tutorials about for loop instructions in all the programming languages covered here at Jenkov.com:
Tweet | |
Jakob Jenkov |