[Tutor] Alternative to for/while loop

Ben Finney ben+python at benfinney.id.au
Tue Oct 25 04:10:00 EDT 2016


Bryon Adams <bryonadams at openmailbox.org> writes:

>     I'm very new to python so please forgive what may be a beginner
> question.

Welcome! You are in the right place for asking beginner Python questions
:-)

> I'm thinking there should be a way to do this without the for loop I
> used, but I'm at a loss here.

Thank you for posting your code, and a specific question about it.

Why do you think there “should be a way to do this without the for
loop”? If you want to do something with a collection of items, a ‘for’
loop is quite a normal way to do that.

(In fact you have not used a for loop, you have used a different syntax
called a “list comprehension”. But the main question remains unchanged:
Why would you expect to not need some kind of iteration like a ‘for’?)

-- 
 \      “Probably the earliest flyswatters were nothing more than some |
  `\    sort of striking surface attached to the end of a long stick.” |
_o__)                                                     —Jack Handey |
Ben Finney



More information about the Tutor mailing list