Home > Blockchain >  Iterating over a dictionary and adding the values
Iterating over a dictionary and adding the values

Time:08-01

dict = {"a": 1,
        "e": 2,
        "i": 3,
        "o": 4,
        "u": 5}

s = "Welcome to Indonesia"

total = 0

for n in s:  

    if n in dict:

    # Add the total number of vowels. Answer - 22

I'm getting multiple errors and getting a bit confused. I'm trying to get the total value for vowels of the string. I decided to make a dict with the key being the vowels and the value being the value of the vowels.

CodePudding user response:

Going in the opposite direction from @sebtheiler's answer, it can be done in a single statement:

vowel = {"a": 1,
         "e": 2,
         "i": 3,
         "o": 4,
         "u": 5}

s = "Welcome to Indonesia"

total = sum(vowel.get(ch, 0) for ch in s)

Perhaps not OP's original approach, but simple and readable. It generates all the vowel values by looping over the characters ch in s, using the default value of 0 if ch is not in vowel and summing the result as total.

If you want to catch uppercase vowels as well (like the I in Indonesia), this works:

total = sum(vowel.get(ch.lower(), 0) for ch in s)

CodePudding user response:

@BeRT2me's answer is completely correct and more efficient; however, the following code is likely more understandable for a beginner and more in line with the original question:

dict = {"a": 1,
        "e": 2,
        "i": 3,
        "o": 4,
        "u": 5}

s = "Welcome to Indonesia"

total = 0

for n in s:  

    if n in dict:

        total  = dict[n]

print(total)  # output: 22

CodePudding user response:

Using dict.get() means we don't have to worry whether the value we're looking up is in the dictionary or not. If it's not, then the default 0 is returned.

vowels = {"a": 1,
          "e": 2,
          "i": 3,
          "o": 4,
          "u": 5}

s = "Welcome to Indonesia"

total = 0
for l in s:
    total  = vowels.get(l, 0)

print(total)

# Output:
22

As for why you shouldn't use dict as a variable name:

  • I should be able to do:
>>> dict((('a', 'b'), ('c', 'd')))
{'a': 'b', 'c': 'd'}

But if we've overwritten the built in dict with a variable, then this is the result, and other important places that use the built in dict may also now fail:

>>> dict((('a', 'b'), ('c', 'd')))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'dict' object is not callable

CodePudding user response:

I think I have fixed your issues your having with dictionaries. Given the goal is to 'get the total value for vowels of the string' I changed the values in your dictionary to 0 to initialize all the keys to 0. Also @BeRT2me comment is correct, 'dict' is a reserved word by python so making a unique variable is better.

dictionary_1 = {"a": 0,
                "e": 0,
                "i": 0,
                "o": 0,
                "u": 0}

s = "Welcome to Indonesia"

total = 0

for n in s:                      #Index through each letter 
                                 #in String 's'
    if n in dictionary_1:        #Check if letter is a key 
                                 #in the dictionary

        dictionary_1[n]  = 1     #Increment the Value in the 
                                 #Dictionary by 1

print(dictionary_1)

The output from the print statement should be what you are looking for.

>> {'a': 1, 'e': 3, 'i': 1, 'o': 3, 'u': 0}
  • Related