Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Updating A List Word Counter

I’m creating a program that counts letters. I created two dictionaries that both have the same word and, because they are the same exact word, both have the same counter. I want to know how to merge the two dictionaries so that it updates the counters as well, but I consistently receive the result "NONE."

word = 'he'
word2 = 'he'
d1 = {}
d2 = {}

for letter in word:
    if letter in d1:
        d1[letter]+=1
    else:
        d1[letter] = 1
print(d1)

#That Outputs: {'h': 1, 'e': 1}


for letter in word2:
    if letter in d2:
        d2[letter]+=1
    else:
        d2[letter] = 1
print(d2)

#That Outputs {'h': 1, 'e': 1}



#That Outputs: None

#I want the outputs {'h': 2, 'e': 2}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can concatenate strings and use a single dictionary:

word1 = 'he'
word2 = 'he'

common_dict = {}
for letter in word1 + word2:
    if letter in common_dict:
        common_dict[letter] += 1
    else:
        common_dict[letter] = 1
print(common_dict)

Also please never use a built-in name as a variable name. In your case, you used dict as a variable name

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading