Is there a Python equivalent of the Haskell ‘let’

You could use a temporary list comprehension

[(barcode(productId), metric(size)) for name, size in [lookup(productId)]][0]

or, equivalently, a generator expression

next((barcode(productId), metric(size)) for name, size in [lookup(productId)])

but both of those are pretty horrible.

Another (horrible) method is via a temporary lambda, which you call immediately

(lambda (name, size): (barcode(productId), metric(size)))(lookup(productId))

I think the recommended “Pythonic” way would just be to define a function, like

def barcode_metric(productId):
   name, size = lookup(productId)
   return barcode(productId), metric(size)
list2 = [barcode_metric(productId) for productId in list]

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)