Timo
Timo

Reputation: 13

Building a case sensitive dictionary in python

I am tasked with creating a dictionary that translates the words from the list lst into Swedish, based on the simple dictionary. If the word is not in the dictionary, just return it as it is. We have to use a function.

Here is the problem: I want to make sure the word "Merry" gets read in lower case, otherwise Python will not realize I am referring to the key "merry" in the dictionary, due to its case sensitivity (thus i.lower()). BUT, I would like the returned value to be "God" and not "god", and I cannot seem to figure it out.

dictionary = {"merry": "god", "christmas": "jul", "and": "och",
              "happy": "gott", "new": "nytt", "year": "ar"}


def translate(lst):
    new_list = []   #create an empty list to start with
    for i in lst: #to shuffle through every word in the list "lst"
        i = i.lower()   #to make sure we don't mess up the translation because of a upper case letter
        if i in dictionary.keys():  #look up in the keys
            new_list.append(dictionary[i])  #we want the value of the key i.
        else:
            new_list.append(i)  #return i if it does not exist in the dictionary.
            

    return new_list

# test
print(translate(['Merry', 'christmas', 'and', 'happy', 'new', 'year', 'mom']))

Upvotes: 0

Views: 337

Answers (2)

Claudio Catterina
Claudio Catterina

Reputation: 337

You can use isupper to check if the first character of the input word is capital and capitalize to capitalize the matching word.

dictionary = {"merry": "god", "christmas": "jul", "and": "och",
              "happy": "gott", "new": "nytt", "year": "ar"}


def translate(lst):
    new_list = []
    for i in lst:
        if i.lower() in dictionary.keys():
            match = dictionary[i.lower()]
            if i[0].isupper(): # Check if the first letter of `i` is a capital letter.
                match = match.capitalize() # Capitalize the matching translation
            new_list.append(match)
        else:
            new_list.append(i)

    return new_list

# test
print(translate(['Merry', 'christmas', 'and', 'happy', 'new', 'year', 'mom']))

>>> ['God', 'jul', 'och', 'gott', 'nytt', 'ar', 'mom']

Upvotes: 1

C_Z_
C_Z_

Reputation: 7796

There are a number of ways you could do this. One is to add the capitalized versions of the words to the dictionary.

dictionary = {"merry": "god", "christmas": "jul", "and": "och",
              "happy": "gott", "new": "nytt", "year": "ar"}
capitalized_dictionary = { key.capitalize():value.capitalize() for key, value in dictionary.items() }
dictionary.update(capitalized_dictionary)



def translate(lst):
    new_list = []   #create an empty list to start with
    for i in lst: #to shuffle through every word in the list "lst"
        # Don't need to convert to lower any more
        if i in dictionary.keys():  #look up in the keys
            new_list.append(dictionary[i])  #we want the value of the key i.
        else:
            new_list.append(i)  #return i if it does not exist in the dictionary.
            

    return new_list

# test
print(translate(['Merry', 'christmas', 'and', 'happy', 'new', 'year', 'mom']))

Upvotes: 0

Related Questions