In this mini tutorial, let’s take a look at some for each python loops for beginners or programmers comming to Python from other programming languages such as C or C++.
So, let’s say you have the following arrays in Python:
pets = ['cat', 'dog', 'fish']
marks = [ 5, 4, 3, 2, 1]
You need to write some code which iterate over the arrays to do some work with the values on each position.
Example 1:
A for loop in Python.
for f in pets:
print f
Example 2:
To iterate over the indices of a sequence, you can combine range()
and len()
as follows:
a = ['Mary', 'had', 'a', 'little', 'lamb']
for i in range(len(a)):
print(i, a[i])
output
0 Mary
1 had
2 a
3 little
4 lamb
Example 3:
When looping through a sequence, the position index and corresponding value can be retrieved at the same time using the enumerate()
function.
for i, v in enumerate(['tic', 'tac', 'toe']):
print(i, v)
output
0 tic
1 tac
2 toe
Reference links:
https://shelbycounty.iowa.gov/vmc/video-ind-v-las-liv-us-nfl301.html
https://shelbycounty.iowa.gov/vmc/video-ind-v-las-liv-us-nfl302.html
https://shelbycounty.iowa.gov/vmc/video-ind-v-las-liv-us-nfl303.html
https://shelbycounty.iowa.gov/vmc/video-ind-v-las-liv-us-nfl304.html
https://shelbycounty.iowa.gov/vmc/video-ind-v-las-liv-us-nfl305.html
https://shelbycounty.iowa.gov/vmc/video-new-v-sea-liv-us-nfl301.html
https://shelbycounty.iowa.gov/vmc/video-new-v-sea-liv-us-nfl302.html
https://shelbycounty.iowa.gov/vmc/video-new-v-sea-liv-us-nfl303.html
https://shelbycounty.iowa.gov/vmc/video-new-v-sea-liv-us-nfl304.html
https://shelbycounty.iowa.gov/vmc/video-new-v-sea-liv-us-nfl305.html
https://shelbycounty.iowa.gov/vmc/video-gre-v-det-liv-us-nfl301.html
https://shelbycounty.iowa.gov/vmc/video-gre-v-det-liv-us-nfl302.html
https://shelbycounty.iowa.gov/vmc/video-gre-v-det-liv-us-nfl303.html
https://shelbycounty.iowa.gov/vmc/video-gre-v-det-liv-us-nfl304.html
https://shelbycounty.iowa.gov/vmc/video-gre-v-det-liv-us-nfl305.html
https://shelbycounty.iowa.gov/vmc/video-sai-v-phi-liv-us-nfl301.html
https://shelbycounty.iowa.gov/vmc/video-sai-v-phi-liv-us-nfl302.html
https://shelbycounty.iowa.gov/vmc/video-sai-v-phi-liv-us-nfl303.html
https://shelbycounty.iowa.gov/vmc/video-sai-v-phi-liv-us-nfl304.html
https://shelbycounty.iowa.gov/vmc/video-sai-v-phi-liv-us-nfl305.html
https://shelbycounty.iowa.gov/vmc/video-atl-v-los-liv-us-nfl301.html
https://shelbycounty.iowa.gov/vmc/video-atl-v-los-liv-us-nfl302.html
https://shelbycounty.iowa.gov/vmc/video-atl-v-los-liv-us-nfl303.html
https://shelbycounty.iowa.gov/vmc/video-atl-v-los-liv-us-nfl304.html
https://shelbycounty.iowa.gov/vmc/video-atl-v-los-liv-us-nfl305.html
https://shelbycounty.iowa.gov/vmc/video-was-v-san-liv-us-nfl301.html
https://shelbycounty.iowa.gov/vmc/video-was-v-san-liv-us-nfl302.html
https://shelbycounty.iowa.gov/vmc/video-was-v-san-liv-us-nfl303.html
https://shelbycounty.iowa.gov/vmc/video-was-v-san-liv-us-nfl304.html
https://shelbycounty.iowa.gov/vmc/video-was-v-san-liv-us-nfl305.html
https://shelbycounty.iowa.gov/vms/video-atl-v-orl-liv-us-nba305.html
https://shelbycounty.iowa.gov/vms/video-bro-v-was-liv-us-nba305.html
https://shelbycounty.iowa.gov/vms/video-det-v-new-liv-us-nba301.html
https://shelbycounty.iowa.gov/vms/video-chi-v-hus-liv-us-nba301.html
https://shelbycounty.iowa.gov/vms/video-los-v-ang-liv-us-nba301.html
https://shelbycounty.iowa.gov/vms/video-por-v-sac-liv-us-nba301.html
https://shelbycounty.iowa.gov/cfn/Col-v-Rad-liv-hstv-11-snf-011.html
https://shelbycounty.iowa.gov/cfn/Col-v-Rad-liv-hstv-11-snf-012.html
https://shelbycounty.iowa.gov/cfn/Col-v-Rad-liv-hstv-11-snf-013.html
https://shelbycounty.iowa.gov/cfn/Col-v-Rad-liv-hstv-11-snf-014.html
https://shelbycounty.iowa.gov/cfn/Col-v-Rad-liv-hstv-11-snf-015.html
https://shelbycounty.iowa.gov/cfn/v-ideo-Jets-liv-setv-cbx-01.html
https://shelbycounty.iowa.gov/cfn/v-ideo-Jets-liv-setv-cbx-02.html
https://shelbycounty.iowa.gov/cfn/v-ideo-Jets-liv-setv-cbx-03.html
https://shelbycounty.iowa.gov/cfn/v-ideo-Jets-liv-setv-cbx-04.html
https://shelbycounty.iowa.gov/cfn/v-ideo-Jets-liv-setv-cbx-05.html
https://shelbycounty.iowa.gov/cfn/Packers-v-Lion-foot-liv-ustv-01.html
https://shelbycounty.iowa.gov/cfn/Packers-v-Lion-foot-liv-ustv-02.html
https://shelbycounty.iowa.gov/cfn/Packers-v-Lion-foot-liv-ustv-03.html
https://shelbycounty.iowa.gov/cfn/Packers-v-Lion-foot-liv-ustv-04.html
https://shelbycounty.iowa.gov/cfn/Packers-v-Lion-foot-liv-ustv-05.html
https://shelbycounty.iowa.gov/cfn/Eagles-v-Saints-liv-ntv-btvchd-01.html
https://shelbycounty.iowa.gov/cfn/Eagles-v-Saints-liv-ntv-btvchd-02.html
https://shelbycounty.iowa.gov/cfn/Eagles-v-Saints-liv-ntv-btvchd-03.html
https://shelbycounty.iowa.gov/cfn/Eagles-v-Saints-liv-ntv-btvchd-04.html
https://shelbycounty.iowa.gov/cfn/Eagles-v-Saints-liv-ntv-btvchd-05.html
https://shelbycounty.iowa.gov/cfn/v-ideo-Falcons-liv-ntv-091.html
https://shelbycounty.iowa.gov/cfn/v-ideo-Falcons-liv-ntv-092.html
https://shelbycounty.iowa.gov/cfn/v-ideo-Falcons-liv-ntv-093.html
https://shelbycounty.iowa.gov/cfn/v-ideo-Falcons-liv-ntv-094.html
https://shelbycounty.iowa.gov/cfn/v-ideo-Falcons-liv-ntv-095.html
https://shelbycounty.iowa.gov/cfn/v-ideo-49ers-v-Wash-hstv-xnf-011.html
https://shelbycounty.iowa.gov/cfn/v-ideo-49ers-v-Wash-hstv-xnf-012.html
https://shelbycounty.iowa.gov/cfn/v-ideo-49ers-v-Wash-hstv-xnf-013.html
https://shelbycounty.iowa.gov/cfn/v-ideo-49ers-v-Wash-hstv-xnf-014.html
https://shelbycounty.iowa.gov/cfn/v-ideo-49ers-v-Wash-hstv-xnf-015.html
https://shelbycounty.iowa.gov/bfm/Bar-v-Lev-viv-rte-01.html
https://shelbycounty.iowa.gov/bfm/Bar-v-Lev-viv-rte-02.html
https://shelbycounty.iowa.gov/bfm/Bar-v-Lev-viv-rte-03.html
https://shelbycounty.iowa.gov/bfm/Bar-v-Lev-viv-rte-04.html
https://shelbycounty.iowa.gov/bfm/Bar-v-Lev-viv-rte-05.html
https://shelbycounty.iowa.gov/bfm/Mil-v-Par-liv-sky8-tv-01.html
https://shelbycounty.iowa.gov/bfm/Mil-v-Par-liv-sky8-tv-02.html
https://shelbycounty.iowa.gov/bfm/Mil-v-Par-liv-sky8-tv-03.html
https://shelbycounty.iowa.gov/bfm/Mil-v-Par-liv-sky8-tv-04.html
https://shelbycounty.iowa.gov/bfm/Mil-v-Par-liv-sky8-tv-05.html
https://shelbycounty.iowa.gov/bfm/Paris-SG-v-OL-rmc-tv-01.html
https://shelbycounty.iowa.gov/bfm/Paris-SG-v-OL-rmc-tv-02.html
https://shelbycounty.iowa.gov/bfm/Paris-SG-v-OL-rmc-tv-03.html
https://shelbycounty.iowa.gov/bfm/Paris-SG-v-OL-rmc-tv-04.html
https://shelbycounty.iowa.gov/bfm/Paris-SG-v-OL-rmc-tv-05.html
https://shelbycounty.iowa.gov/vmx/video-Colts-v-Raiders-btv01.html
https://shelbycounty.iowa.gov/vmx/video-Colts-v-Raiders-btv02.html
https://shelbycounty.iowa.gov/vmx/video-Colts-v-Raiders-btv03.html
https://shelbycounty.iowa.gov/vmx/video-Colts-v-Raiders-btv04.html
https://shelbycounty.iowa.gov/vmx/video-Colts-v-Raiders-btv05.html
https://shelbycounty.iowa.gov/vmx/Video-Jets-v-Seahawks-nvc1.html
https://shelbycounty.iowa.gov/vmx/Video-Jets-v-Seahawks-nvc2.html
https://shelbycounty.iowa.gov/vmx/Video-Jets-v-Seahawks-nvc3.html
https://shelbycounty.iowa.gov/vmx/Video-Jets-v-Seahawks-nvc4.html
https://shelbycounty.iowa.gov/vmx/Video-Jets-v-Seahawks-nvc5.html
https://shelbycounty.iowa.gov/vmx/HQ-Packers-v-Lions-ntc01.html
https://shelbycounty.iowa.gov/vmx/HQ-Packers-v-Lions-ntc02.html
https://shelbycounty.iowa.gov/vmx/HQ-Packers-v-Lions-ntc03.html
https://shelbycounty.iowa.gov/vmx/HQ-Packers-v-Lions-ntc04.html
https://shelbycounty.iowa.gov/vmx/HQ-Packers-v-Lions-ntc05.html
https://shelbycounty.iowa.gov/vmx/video-sai-v-eag-4k-tv08-01.html
https://shelbycounty.iowa.gov/vmx/video-sai-v-eag-4k-tv08-02.html
https://shelbycounty.iowa.gov/vmx/video-sai-v-eag-4k-tv08-03.html
https://shelbycounty.iowa.gov/vmx/video-sai-v-eag-4k-tv08-04.html
https://shelbycounty.iowa.gov/vmx/video-sai-v-eag-4k-tv08-05.html
https://shelbycounty.iowa.gov/vmx/Video-Falcons-v-Chargers-nf01.html
https://shelbycounty.iowa.gov/vmx/Video-Falcons-v-Chargers-nf02.html
https://shelbycounty.iowa.gov/vmx/Video-Falcons-v-Chargers-nf03.html
https://shelbycounty.iowa.gov/vmx/Video-Falcons-v-Chargers-nf04.html
https://shelbycounty.iowa.gov/vmx/Video-Falcons-v-Chargers-nf05.html
https://shelbycounty.iowa.gov/vmx/video-was-v-49r-4k-tv-01.html
https://shelbycounty.iowa.gov/vmx/video-was-v-49r-4k-tv-02.html
https://shelbycounty.iowa.gov/vmx/video-was-v-49r-4k-tv-03.html
https://shelbycounty.iowa.gov/vmx/video-was-v-49r-4k-tv-04.html
https://shelbycounty.iowa.gov/vmx/video-was-v-49r-4k-tv-05.html
https://shelbycounty.iowa.gov/vmx/Video-barcelona-vs-levante-Sfsja01.html
https://shelbycounty.iowa.gov/vmx/Video-barcelona-vs-levante-Sfsja02.html
https://shelbycounty.iowa.gov/vmx/Video-barcelona-vs-levante-Sfsja03.html
https://shelbycounty.iowa.gov/vmx/Video-barcelona-vs-levante-Sfsja04.html
https://shelbycounty.iowa.gov/vmx/Video-barcelona-vs-levante-Sfsja05.html
https://shelbycounty.iowa.gov/vmx/video-psg-v-lyon-uu-fr01.html
https://shelbycounty.iowa.gov/vmx/video-psg-v-lyon-uu-fr02.html
https://shelbycounty.iowa.gov/vmx/video-psg-v-lyon-uu-fr03.html
https://shelbycounty.iowa.gov/vmx/video-psg-v-lyon-uu-fr04.html
https://shelbycounty.iowa.gov/vmx/video-psg-v-lyon-uu-fr05.html
https://shelbycounty.iowa.gov/vmx/video-c-psg-ol-lyon-x-v1.html
https://shelbycounty.iowa.gov/vmx/video-psg-v-lyon-uu-fr02.html
https://shelbycounty.iowa.gov/vmx/video-psg-v-lyon-uu-fr03.html
http://frenchdigitaltour.org/krox/video-psg-v-lyon-uu-fr01.html
http://frenchdigitaltour.org/krox/video-psg-v-lyon-uu-fr02.html
http://frenchdigitaltour.org/krox/video-psg-v-lyon-uu-fr03.html
http://frenchdigitaltour.org/krox/video-psg-v-lyon-uu-fr04.html
http://frenchdigitaltour.org/krox/video-psg-v-lyon-uu-fr05.html
http://frenchdigitaltour.org/krox/video-c-psg-ol-lyon-x-v1.html
http://frenchdigitaltour.org/krox/video-psg-v-lyon-uu-fr02.html
http://frenchdigitaltour.org/krox/video-psg-v-lyon-uu-fr03.html
If you are a beginner learning Python programming, then I recommend taking a look at the below resources before continuing with the rest of the post.
Get the Book: Python Programming for beginners
Python for Beginners: Learn Python in One Day
Now let’s say you have mastered the basics of Python and you would like to build your own For Each function to loop over the arrays in your python code.
def forEach(list, function):
for i, v in enumerate(list):
function(v, i, list)
You could also adapt this to map
, reduce
, filter
, and any other array functions from other languages or precedence you'd want to bring over.
Example 4: Iterating over a dictionary in Python {key:value}
for key, value in Dictionary.items():
print(key, value)
If you want to stringify all the keys of the dictionary , you can go ahead and try:
stringified_dictionary = {}
for key, value in Dictionary.items():
stringified_dictionary.update({str(key): str(value)})
return stringified_dictionary
Example 5:
dictionary = {'a': 0, 'z': 25}
for index, (key, value) in enumerate(dictionary.items()):
## Code here ##
Hope all of these examples are useful for you and you had fun implementing them in your code.