Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read this lambda x: f'{x:0>6}'

Tags:

python

I am new to python, how to read f'{x:0>6}' in the following code

df[col].map(lambda x: f'{x:0>6}')

I found f meant f-string. But I don't understand what the whole expression mean.

like image 610
spkjess Avatar asked Dec 07 '25 14:12

spkjess


1 Answers

It means right aligned front pad zeros for any value of x if x has less than 6 digits:

s = [(lambda x: f'{x:0>6}')(num) for num  in [4, 45, 123456]]
print(s)
# ['000004', '000045', '123456']

lambda is a nameless function that is used mostly on fly: The lambda expression is equivalents to

def padd_zero(x):
    return f'{x:0>6}'

s = [padd_zero(num) for num  in [4, 45, 123456]]
print(s)
# ['000004', '000045', '123456']

In the example df[col].map(abc). abc is applied to all values in col, just as we apply the functions to the list above.

See: Strings Docs 3.6+

like image 69
Prayson W. Daniel Avatar answered Dec 09 '25 03:12

Prayson W. Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!