Don't understand Python lambda function syntax

Viewed 111

I don't understand how this syntax work and can't find documentation on it:

word = '123xyz567'
s = ''.join (c if c.isdigit() else ' 'for c in word)

Line 2 of code will take xyz123 and join together "123 567" into one string by passing in a lambda function to join

However, I am confused as to how this syntax works, normally a python if-else statement goes:

def example():
    for c in word:
        if c.isdigit():
            return c
        else:
            return ' ' 

Can someone either explain or direct me to documentation that explains to me the syntax structure of the lambda function passed into .join() and how I can use it properly?

3 Answers

That's not a lambda but a generator expression.

https://www.python.org/dev/peps/pep-0289/

Almost like a list comprehension but instead a generator literal.

list = [a for a in [1,2,3]]
generator = (a for a in [1,2,3])
print(list) # [1,2,3]
print(generator) # <generator object <genexpr> at 0x7f8ee3abd6d0>

There are several things goining on here, none of them are lambda.

The first thing is a generetor expresion, which is of the form: (<expresion> for <var> in <iterable>).
In your example <expresion> is c if c.isdigit() else ' ', <var> is c, and iterable is word.
It creates a generator (which is a sort of iterable) to pass to str.join.

The second thing is a ternary operator, which is of the form: <expresion> if <bool-expresion> else <expresion>.
In your example the first <expresion> is c, <bool-expresion> is c.isdigit(), and the second <expresion> is ' '.
It returns the first <expresion> if the <bool-expresion> evaluates to True, else it returns the second <expresion>.

Everything put together, (c if c.isdigit() else ' 'for c in word) is a generator with all the characters of word but with non-digit characters replaced with a single space (each).

Related