[Tutor] using a for loop in another method
Tim Golden
mail at timgolden.me.uk
Fri Apr 22 15:44:30 EDT 2016
On 22/04/2016 17:08, Rene.Castillo wrote:
> Hi, this is my first post on this mailing list-
>
> I wanted to ask about this type of execution in python,
>
> expected output-
> reverse_words("This is an example!") # returns "sihT si na !elpmaxe"
>
>
> below is my execution, followed by another persons execution, which i dont
> completely understand.
>
> def reverse_words(strng):
> strng = strng[::-1].split(' ')
> strng.reverse()
> return ' '.join(strng)
>
>
> def reverse_words(s)
> return ' '.join(s[::-1] for s in str.split(' '))
>
>
>
> how can a for loop be called within another method like that- and possibly
> what situations does this benefit
> i don't even know how to google this sort of thing
> any words are appreciated-
It's tricky, isn't it? Once I give you the magic phrase, you'll find
examples and explanations all over the place:
list comprehension
(strictly, the example you're showing is a "generator comprehension" but
you'll probably get more hits for the slightly older "list comprehension").
TJG
More information about the Tutor
mailing list