Returning MIME types according to input filenames' extensions I am coding on codingame.com, where one of the 'easy' challenges is to assign MIME type strings to input strings of file names based on their extensions.

My code passes four out of five of the test cases, and the last one is an optimization test. A big data set is inputted, and my approach times out. 

I started coding in python under a year ago, and I haven't had much practice. I would like to know what I could do to improve my code.

I have seen really short solutions to other puzzles in the 'other solutions' tab, so I am not surprised if there is a clever workaround to make this shorter.

import sys
import math

n = int(input())  # Number of elements which make up the association table.
q = int(input())  # Number Q of file names to be analyzed.


MIMETable = {}
fileNames = {}
extensions = {}
answer = ''


#extract association input to table
for i in range(n):
    # ext: file extension
    # mt: MIME type.
    ext, mt = input().split()
    MIMETable [ext.lower()] = mt
    #print(str(ext) + '\t|   ' + str(mt), file=sys.stderr)

#print('\n', file=sys.stderr)

#extract filename input
for i in range(q):
    fname = input()  # One file name per line.
    fileNames[i] = fname
    #print(str(i) + ' ' + fname, file=sys.stderr)




#   find the extensions of the filenames 
#   and add them to extensions{} in lowercase as they are in MIMETable
for index, name in fileNames.items():
    if '.' in name:
        try:
            extensions[index] = name.split('.')[-1].lower() 
        except IndexError:
            extensions[index] = 'unknown'
    else:
        extensions[index] = 'unknown'
    #print(extensions[index], file=sys.stderr)


#print('', file=sys.stderr)


#if there is an extension, find the corresponding MIME type
for fileIndex in range(q):
    extensionFound = False
    for mimeExtension, mimetype in MIMETable.items():
        if extensions[fileIndex] == mimeExtension:
            answer += mimetype
            extensionFound = True
            #print('extension found: ' +  extensions[fileIndex] + '\t' + mimetype, file=sys.stderr)
    if not extensionFound:
        answer += 'UNKNOWN'
    answer += '\n'


print('\n', file=sys.stderr)

# For each of the Q filenames, display on a line the corresponding MIME type. If there is no corresponding type, then display UNKNOWN.
print(answer)
You should use a list rather than a dictionary for fileNames or extensions.
You don't get any benefit for using a dictionary,
and actually makes the code slightly harder to use.
You should utilize a dictionary's \$O(1)\$ key lookup, rather than re-implement an \$O(n)\$ look up.
This is as if extensions[fileIndex] in MIMETable is already fast.
Utilize dictionary functions such as get(key, default).
You don't need three separate loops, as they are all just different sections of transforming from a file name to a MIME type.
It would also be easier to read it without the loops too.
You should follow PEP8, this means your names should be MIME_table rather than MIMEtable.
Also MIME_table looks odd, so I'd change it to mime_table.
You don't need math or sys, and so I'd remove them.
Your comments are left overs to debugging,
instead of commenting out prints when you're debugging,
I'd recommend that you use Pythons logging library.


Without merging the loops together, you can change your code to:

amount_mime_types = int(input())
amount_file_names = int(input())
mime_table = {}
for i in range(amount_mime_types):
    ext, mt = input().split()
    mime_table[ext.lower()] = mt

file_names = []
for _ in range(amount_file_names):
    file_names.append(input())

extensions = []
for name in file_names:
    if '.' in name:
        extensions.append(name.split('.')[-1].lower())
    else:
        extensions.append('unknown')

mime_types = []
for extension in extensions:
    mime_types.append(mime_table.get(extension, 'UNKNOWN'))

print('\n'.join(mime_types))


Where if you join the file_names, extensions and mime_types loops together you get:

amount_mime_types = int(input())
amount_file_names = int(input())
mime_table = {}
for i in range(amount_mime_types):
    ext, mt = input().split()
    mime_table[ext.lower()] = mt

mime_types = []
for _ in range(amount_file_names):
    name = input()
    if '.' in name:
        extension = name.split('.')[-1].lower()
    else:
        extension = 'unknown'
    mime_type = mime_table.get(extension, 'UNKNOWN')
    mime_types.append(mime_type)

print('\n'.join(mime_types))


There are still a couple ways to improve this:


You can write all this in a function.
This also improves the speed of the program.
Pre-define mime_types to an 'array' of None. mime_types = [None] * amount_file_names.
This is as lists sometimes have to move and is more a 'just in case' optimisation.