O
O
Onigire2021-06-05 21:34:44
Python
Onigire, 2021-06-05 21:34:44

Is it possible to optimize this code?

I can write code, but now I am learning to write competent and optimized code.
This program takes any word and writes to the dictionary how many times each letter of the word was repeated, but is it possible to write better (shorter) using for example **kwargs?

word = input()
lib = {}
for letter in word:
    lib[letter] = word.count(letter)
print(lib)

# hello
# {'h': 1, 'e': 1, 'l': 2, 'o': 1}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sswwssww, 2021-06-05
@Onigire

from collections import Counter
lib = Counter(input('Введите слово:\n'))

Y
Yupiter7575, 2021-06-05
@yupiter7575

Dictionary generator ,_,

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question