How to categorizing a list of words by their first letters as a dictionary of lists in Python

words = ["python","java","php","javascript"]
with_letter = {}
for word in words:
    letter = word[0]
    if letter not in with_letter:
        with_letter[letter] = [word]
    else:
        with_letter[letter].append(word)

print(with_letter)

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *