I've made a function to turn a MM/DD/YYYY
date into a DD-MMM-YYYY
date (e.g. 05/03/2023
to 03-MAY-2023
), but in the function I use date.split('/')
twice and I'd prefer to only use it once.
I know I could use date = date.split('/')
before the lambda function, I was just wondering if there is a way to do that inline with a lambda function?
# Current code
# Given vars
months = ['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC']
date = "05/03/2023"
# Function
newDate = lambda date : '-'.join([date.split('/')[i] if i != 0 else months[int(date.split('/')[i])-1] for i in [1,0,2]])
# Function I want
newDate = lambda (lambda date : date.split('/'))(date) : '-'.join([date[i] if i != 0 else months[int(date[i])-1] for i in [1,0,2]])
# But this gives a syntax error