Search code examples
pythontuplesinline-functions

Python - Inline get one member of tuple from delegate


From a previous question functions cannot be defined inline in a dictionary object.

However, I just have this simple case,

def getExtension(fileName):
    return os.path.splitext(fileName)[1]

funcDict = { 
    'EXT': getExtension,  # maybe os.path.splitext[1]?
    }

print(funcDict['EXT']('myFile.txt'))

Is there someway to specify that I only want one element of the tuple returned by os.path.splitext so I can get out of defining getExtension?


Solution

  • funcDict = {
        'EXT': (lambda x: os.path.splitext(x)[1])
    }