Thanks for the many replies. I apologise for the original message which was obviously very unclear - I will try to correct that. Basically I want to match a telephone number to a rate table. Assume the following rate table: Telephone code Rate 649 1 6421 2 6422 2.5 64 1.5 The following table sets out the appropriate rate to return for a given telephone number Dialled number Returned rate 649123 1 643123 1.5 6421111 2 64221 2.5 Things to note: If there is no match the right-most digit is removed until there is a match e.g. 643123 matches to 64 The length of the "dialled number" is not consistent e.g. 649123 vs 64221 The length of the "telephone code" is not consistent e.g. 64 vs 6422 I want to do this frequently and at low cost i.e. ideally in memory. Perhaps the best way is to write a procedure in MySQL however I am interested in any python-based alternatives. Regards Cameron