2

I want to improve my code's performance and running time, looking for write my loops better.

For example, I have a dictionary that contains words as keys, and their translation in Spanish as values.

{
    'Hello' : 'Hola',
    'Goodbye' : 'Adios',
    'Cheese' : 'Queso'
}

I also have a given English sentence, and I want to iterate over any word in my dict and replace it with the Spanish translation. For this scenario I consider that up to one word could be exist in the given sentence.

I wrote a basic code that do that, but I am not sure that it is best practice:

words_list = {
        'Hello' : 'Hola',
        'Goodbye' : 'Adios',
        'Cheese' : 'Queso'
    }
sentence = "Hello, I want to talk Spanish"

for english_word in words_list.keys():
    if english_word in sentence:
        sentence = sentence.replace(english_word, words_list[english_word])
        break

print sentence

How can I write it better?

Thanks!

Dan
  • 829
  • 2
  • 12
  • 23
  • 2
    This will be more suitable for [CodeReviewSE](https://codereview.stackexchange.com). – Ashwini Chaudhary Aug 29 '17 at 14:42
  • 7
    I'm voting to close this question as off-topic because it belongs to https://codereview.stackexchange.com/ – Mike Scotty Aug 29 '17 at 14:44
  • I copied it to there, thanks – Dan Aug 29 '17 at 14:45
  • 1
    @Dan If you close this question yourself (here on SO) it will speed up the process. Otherwise it will most likely be closed through the review process. Cheers =) – Mike Scotty Aug 29 '17 at 14:47
  • 1
    Is this really off-topic for SO just because their code works? Many good SO questions include working code searching improvements and the title makes a reasonable SO question (with coding attempt) – Chris_Rands Aug 29 '17 at 14:51
  • 1
    @Chris_Rands It may not be *completely* off topic here (imo it is) but in any case it is certainly *more* on topic on CodeReview. Put questions where they best belong. – bendl Aug 29 '17 at 14:56
  • 1
    @Chris_Rands It's probably not totally off-topic, but it suits CodeReview better. [reference_1](https://meta.stackexchange.com/a/102869) [reference_2](https://meta.stackexchange.com/a/211790) - and a question should only be posted on one of the sites, or am I wrong? AFAIK that's why mods even have the ability to move questions. – Mike Scotty Aug 29 '17 at 14:59
  • @mpf82 For example, no-one suggested [this](https://stackoverflow.com/questions/845058/how-to-get-line-count-cheaply-in-python) go to Code Review, despite the working code in the question. Plus migration is normally rather slow – Chris_Rands Aug 29 '17 at 15:04
  • @Chris_Rands as I understand, CodeReview is a spin-off of SO. Not sure it even existed in 2009. And old questions don't play by the current rules/gidelines anyway (e.g. there are a lot of "Recommend me a library for X" questions, that nowadays would instantly be closed). – Mike Scotty Aug 29 '17 at 15:11
  • @mpf82 CodeReview first questions were in 2008. Anyway, my point is [that](https://stackoverflow.com/questions/845058/how-to-get-line-count-cheaply-in-python) question is good for SO despite the working code (and so are many other questions). I guess there's just an overlap/grep area between the forums – Chris_Rands Aug 29 '17 at 15:14

0 Answers0