This a solution for Project Euler Problem 185.  It works fine for the example but slow for the problem statement. How can I improve its speed and efficiency?import itertoolsfrom collections import defaultdictdef mrange(start, stop, step=1):    i = start    while i < stop:        yield i        i += step#produces the list of numbersnumber= mrange(10**4,10**5)    newlist=[]   def combination(x,n):        if n==0:        temp=[]        for i in range(1,len(x)+1):                temp.extend(list(itertools.combinations(x,i)))           return temp        return list(itertools.combinations(x,n))def combineDictTuple(dt):        d = {}    for item in dt:        d.update(item)    return d   def prob(a,n):    temp=[]    if n==0:        temp=number    #modchar is indexing the numbers eg:[{0: '9'}, {1: '0'}, {2: '3'}, {3: '4'}, {4: '2'}]    modchar=[{i: c} for i, c in enumerate(a)]    combos = combination(modchar,n)    newCombos = [combineDictTuple(dt) for dt in combos]    # newCombos gives combination of the indexes     #[{0: '9', 1: '0'}, {0: '9', 2: '3'}, {0: '9', 3: '4'}, {0: '9', 4: '2'}, {1: '0', 2: '3'}, {1: '0', 3: '4'}, {1: '0', 4: '2'}, {2: '3', 3: '4'}, {2: '3', 4: '2'}, {3: '4', 4: '2'}]        for j in number:        for i in newCombos:            index=i.keys()            val=i.values()            # print index,val            if n==0:                if all(str(j)[index[l]]==val[l] for l in range(0,len(val))):                    if j in temp:                        del temp[temp.index(j)]                        break            else:                if all(str(j)[index[l]]==val[l] for l in range(0,len(val))):                    temp.append(j)                    break           return temp    val='''90342 ;2 correct70794 ;0 correct39458 ;2 correct34109 ;1 correct51545 ;2 correct12531 ;1 correct'''   val=val.replace('correct','').replace(';','').split('\n')for i in val:        i=i.split()    number=prob(i[0],int(i[1]))print numberHow the function works:Generate numbers using mrange()In prob method  first I find the index of the each number in problem statement, e.g. 90342 That produces [{0: '9'}, {1: '0'}, {2: '3'}, {3: '4'}, {4: '2'}]Using n (n is no. of correct values) I produce combinations of the above dict example as stated in commentsFor every number I am checking the condition which satisfy the index:value append into temp and at last my new numbers become temp values. This iterates for all values until it arrives at the answer.note: if n=0. i calculate all the combinations and delete the numbers from temp which satisfy that.
Improving efficiency of Project Euler 185 (16-place Mastermind)
python;mathematics;programming challenge;combinatorics