4 Python Looping Techniques You Should Know

Useful looping methods in Python

Posted by Prince Jan on February 23, 2021

Python loops are pretty awesome. I’ve never been a big fan of loops because I always thought they were too complicated and I didn’t want to spend my time figuring out “the right way” to do them.
Fortunately, I think loops have become more intuitive in recent versions of Python, and I’ve learned a few new tricks that I want to share with you.

In this post, I’m going to share 4 python looping techniques that I find to be the most useful.

1. Looping over two lists at the same time

Have you ever wanted to loop over two lists at the same time?. This can be achieved using the zip() function.

colors = ["red", "blue", "white"]
types = ["warm", "cool", "neutral"]

for c, t in zip(colors, types):
    print(c + " is " + t)

Program Output:

red is warm
blue is cool
white is neutral

 

 

2. Using the enumerate() function.

When looping through a sequence (lists, tuple, string, etc), the position index and corresponding value can be retrieved at the same time using the enumerate() function.

colors = ["orange", "brown", "indigo", "black"]

for i, v in enumerate(colors):
    print(i , v)

Program Output:

0 orange
1 brown
2 indigo
3 black

 

The enumerate() function returns individual elements in the list with their indexes.

 

 

3. Looping through a dictionary with the items() method.

We can get both the keys and the corresponding values when looping over a dictionary by using the items() method.

grades = {'Ana': 'B', 'John':'A+', 'Denise':"A", "katy": 'A'}

for name, g in grades.items():
    print(name + " had " + g)

Program Output:

Ana had B
John had A+
Denise had A
katy had A

 

 

4. Reverse looping in Python.

To loop over a range of numbers in reverse order, first, specify the range and then call the reversed() function.

for r in reversed(range(7)):
    print(r)

Program Output:

6
5
4
3
2
1
0

 

The same analogy applies to looping over a list in reverse:

colors = ["blue", "red", "black", "yellow"]

for i in reversed(colors):
    print(i)

Program Output:

yellow
black
red
blue

 

Instead of implementing your own way of looping over two lists at the same time or looping reversely, these functions can save you lots of time. Knowing these little techniques and using them in your day-to-day programming can make you an efficient programmer. If you want to learn more Python, check out the best python courses on Udemy.


Subscribe to receive updates!

1 Comments

avatar

Anonymous March 15, 2021

I for one would never use reversed() in that case.

range(7) is short for range(0:7)
>>>for r in range(0:7): print(r)
prints 0...6 but your reversed() approach comes up with 7...1.

So instead of of reversed() I use
>>>for r in range(0,7,-1): print(r)
which prints 6...0. Way more pythonic imho.

Same for a list:
>>>for i in colors[::-1]: print(r)
Although there's no difference in the output it still seems more consistent and pythonic to me than using reversed.

Just my 2 cents.


avatar

Prince Jan March 16, 2021

Thanks for sharing. Though using your method seems more pythonic, using the reversed() function is simple and clean.


Leave a comment

* Email would not published