9

While learning python I came across a line of code which will figure out the numbers of letters.

dummy='lorem ipsum dolor emet...'
letternum={}

for each_letter in dummy:
    letternum[each_letter.lower()]=letternum.get(each_letter,0)+1

print(letternum)

Now, My question is -in the 4th line of code inletternum.get(each_letter,0)+1 why there is ,0)+1and why is it used for. Pls describe.

3
  • 6
    There is no better explanation than dict.get. Commented Apr 10, 2020 at 13:17
  • 1
    For learning how to use .get() that code is fine, but this particular type of dictionary is so common that (in effect) it is included as Counter in the collections module. Simply Counter(dummy) does basically the same thing as your code. Commented Apr 10, 2020 at 13:23
  • 1
    @ZuhairAbid It's a way to have a default value instead of going through the hassle of pre-defining all possible cases. Commented Apr 10, 2020 at 13:23

6 Answers 6

10

The get method on a dictionary is documented here: https://docs.python.org/3/library/stdtypes.html#dict.get

get(key[, default])

Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.

So this explains the 0 - it's a default value to use when letternum doesn't contain the given letter.

So we have letternum.get(each_letter, 0) - this expression finds the value stored in the letternum dictionary for the currently considered letter. If there is no value stored, it evaluates to 0 instead.

Then we add one to this number: letternum.get(each_letter, 0) + 1

Finally we stored it back into the letternum dictionary, although this time converting the letter to lowercase: letternum[each_letter.lower()] = letternum.get(each_letter, 0) + 1 It seems this might be a mistake. We probably want to update the same item we just looked up, but if each_letter is upper-case that's not true.

Sign up to request clarification or add additional context in comments.

Comments

3

letternum is a dict (a dictionary). It has a method called get which returns the value associated with a given key. If the key is absent from the dictionary, it returns a default value, which is None unless an optional second argument is present, in which case that argument value is returned for missing elements.

In this case, letternum.get(each_letter,0) returns letternum[each_letter] if each_letter is in the dictionary. Otherwise it returns 0. Then the code adds 1 to this value and stores the result in letternum[each_letter.lower()].

This creates a count of the number of occurrences of each letter, except that it inconsistently converts the letter to lowercase when updating, but not when retrieving existing values, so it won't work properly for uppercase letters.

Comments

0

According to the docs in https://www.tutorialspoint.com/python/dictionary_get.htm, the second parameter to the get method for dictionaries is an optional parameter, which specifies the default value to be outputted in case the key is not in the dictionary. For example:

letternum = {'keyone':1,'keytwo':2}
print(letternum.get('keythree',3))
print(letternum.get('keyone',3))

would output 3 then 1 because keythree doesn't exist in the dictionary, so it outputs 3 in the first print statement, then 1 in the second print statement, because the key exists.

Comments

0

.get() simplifies cases of missing object attributes. Instead of dealing with None and exception handling when a missing attribute is invoked in letternum the get() is used to return a default zero value

Comments

0

The dict.get() is used to return the value of a key from de dictionary and can take one to two arguments, being the first the key and the second (optional) the value to be returned if the key is not found. If this value is not specified it will return None when key is not found.

In your program, if the key each_letter is not in letternum the get method will return 0 and then add 1 for the counting.

Comments

0

If each_letter is not found in letternum, each_letter: 0 is inserted into letternum. Else, the value from letternum[each_letter.lower()] +1 is inserted. collections.Counter() is better for this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.