Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

How to Write Pythonic Loops: Conclusion

In this course, you learned how to write loops like a Python developer. Keep in mind that writing C-style loops is considered not Pythonic. If possible, try to avoid managing loop indexes and stop conditions manually.

Also remember that for loops in Python are like “for each” loops in other languages, so you can use them to iterate over a container or sequence directly.

Avatar image for Abby Jones

Abby Jones on July 16, 2019

Very informative as usual!

Avatar image for Vignesh

Vignesh on July 16, 2019

Good one, making me think in simpler way

Avatar image for Daniel Galvan

Daniel Galvan on July 19, 2019

Thank you Dan! Great refresher

Avatar image for aradim

aradim on July 20, 2019

Thank you very much, Nice!

Avatar image for Peter Ott

Peter Ott on July 22, 2019

👌

Avatar image for Beni

Beni on July 22, 2019

Thank you~ So benefit, …^*^

Avatar image for Rob H

Rob H on July 23, 2019

Great tip, thanks much Dan!

Avatar image for David Mellor

David Mellor on July 25, 2019

Brilliant - I knew this already, but to add a perspective of another language was very useful!

Avatar image for ALXTheMaster

ALXTheMaster on July 31, 2019

Thanks A lot

Avatar image for Balaje

Balaje on Aug. 1, 2019

Awesome! Just a quick question, to develop the decremented logic in for loop, should I provide step parameter with the negative value? Can you please suggest me

Avatar image for brunofl

brunofl on Aug. 4, 2019

This was perfect, I ways had doubts on how to properly implement a c / java style loop the pythonic way :)

Avatar image for Lijo Joseph

Lijo Joseph on Aug. 11, 2019

Loved it!!

Avatar image for Anonymous

Anonymous on Aug. 15, 2019

Thanks Dan! Python does have a way of making things simple and beautiful!

Avatar image for Dan Bader

Dan Bader RP Team on Aug. 15, 2019

Python does have a way of making things simple and beautiful!

I agree! That’s why I find it such an enjoyable language to work with :)

Avatar image for Pygator

Pygator on Dec. 22, 2019

I will laugh at my friends who write c-style loops of course! Enumerate is very useful at getting that index to use if required.

Avatar image for arjunaraoj

arjunaraoj on Dec. 27, 2019

nice

Avatar image for alanhrosenthal

alanhrosenthal on March 24, 2020

Thanks. As an Old C Guy, this was very informative. It will probably take a bit of work to overcome my bad habits.

Avatar image for Ajay

Ajay on April 25, 2020

loved this course, very informative.

Avatar image for Tobi Olusa

Tobi Olusa on June 2, 2020

Quite enlighten.

Avatar image for beingpython

beingpython on Aug. 23, 2020

Being Pythonic… nice message, interesting content.

Avatar image for Ghani

Ghani on Oct. 15, 2020

Very interesting tutorial; thanks Dan!

Avatar image for Alexy

Alexy on Dec. 9, 2020

nice, clear, thanks alot )

Avatar image for JeremyMechEng

JeremyMechEng on Aug. 15, 2022

Thanks for the info!

This may well be covered elsewhere but I’d like to know if nested loops are treated the same? Do they HAVE to be across two lines, or is there a magic one-liner method?

I tried:

my_list = ['a', 'b', 'c']
my_num = [1, 2, 3]

for (item, num) in (my_list, my_num):
    print(item, num)

and found ValueError: too many values to unpack (expected 2)

whereas:

for items in my_items:
    for num in my_num:
        print(items, num)

Yielded the expected result, anyone able to shed some light on this one? Cheers (:

Avatar image for Geir Arne Hjelle

Geir Arne Hjelle RP Team on Aug. 15, 2022

Hi @JeremyMechEng

Python doesn’t have dedicated syntax for nested for loops. However, you can use itertools.product() to nest your lists and then loop over that product:

import itertools

characters = ["a", "b", "c"]
digits = [1, 2, 3]

for char, digit in itertools.product(characters, digits):
    print(char, digit)

realpython.com/python-itertools/#dealing-a-deck-of-cards shows another example of using itertools.product().

Avatar image for Martin Breuss

Martin Breuss RP Team on Aug. 16, 2022

@JeremyMechEng or you could use a list comprehension:

>>> characters = ["a", "b", "c"]
>>> digits = [1, 2, 3]
>>> [print(c, d) for c in characters for d in digits]
a 1
a 2
a 3
b 1
b 2
b 3
c 1
c 2
c 3
[None, None, None, None, None, None, None, None, None]

Note that in this case it’ll create a list with the return value of calling print(), which is None. So it’ll be more interesting if you want to build a list.

Also, complex list comprehensions get messy and hard to understand quickly, so it’s rarely worth it to go into the level of nested loops. It’ll be more readable to write out the nested for loop instead.

Avatar image for JeremyMechEng

JeremyMechEng on Aug. 19, 2022

@Geir Arne Hjelle @Martin Breuss Thanks both for the answers! Much appreciated

Become a Member to join the conversation.