Unscramble words faster in Python So I'm currently using python to unscramble words that are retrieved from an OCR program, and checked in the 'dictionary'. 

The current code I am using to unscramble words is this:

import numpy as nm 
import pytesseract 
import cv2 
import ctypes
from PIL import ImageGrab 

def imToString(): 

    # Path of tesseract executable 
    pytesseract.pytesseract.tesseract_cmd =r'C:\Program Files (x86)\Tesseract-OCR\tesseract'
    while(True): 

        # ImageGrab-To capture the screen image in a loop. 
        # Bbox used to capture a specific area. 
        cap = ImageGrab.grab(bbox =(687, 224, 1104, 240))


        # Converted the image to monochrome for it to be easily 
        # read by the OCR and obtained the output String. 
        tesstr = pytesseract.image_to_string( 
                cv2.cvtColor(nm.array(cap), cv2.COLOR_BGR2GRAY), 
                lang ='eng') 
        checkWord(tesstr)

def checkWord(tesstr):

    dictionary =['orange', 'marshmellow']

    scrambled = tesstr
    for word in dictionary:
        if sorted(word) == sorted(scrambled):
            print(word)


imToString() 


I want to know if there is anyway to reduce the time it takes to look through the 'dictionary', as there are many more words to go through.
Like it was mentioned in the comments, it's difficult for us to tell you how to get your code faster witbout more information about the runtime context of your code. But based on what you have shown in code, I would do these modifications:

import numpy as nm 
import pytesseract 
import cv2 
import ctypes
from PIL import ImageGrab 

def im_to_string(): 

    # Path of tesseract executable 
    pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract'
    while(True): 

        # ImageGrab-To capture the screen image in a loop. 
        # Bbox used to capture a specific area. 
        cap = ImageGrab.grab(bbox=(687, 224, 1104, 240))

        # Converted the image to monochrome for it to be easily 
        # read by the OCR and obtained the output String. 
        tes_str = pytesseract.image_to_string( 
                cv2.cvtColor(nm.array(cap), cv2.COLOR_BGR2GRAY), 
                lang ='eng') 
        check_word(tes_str)

words_dictionary = ['orange', 'marshmellow']
scrambled_words_dictionary = set(sorted(current_word) for current_word in words_dictionary)

def check_word(tes_str):
    if sorted(tes_str) in scrambled_words_dictionary:
        print(tes_str)

im_to_string() 


This is based ont the assumption that the word dictionary doesn't change between screen grabs.

Here's some of the modifications I made to your code:


I defined the word dictionary outside of check_word() so that it doesn't need to be recreated each time to invoke that function
I created a set with the words scrambled because a set is optimized to check for membership. You are also only cheking against the scrambled words
I also changed the name of the dictionary variable because a dictionary has a specific meaning in Python and that could be slightly confusing for someone reading your code


If it's not what you were looking for, give us more precisions.