create a dict of vowels and consonants in python

To create a dictionary of vowels and consonants in Python, you can iterate over each letter of the alphabet and check if the letter is a vowel or consonant, and then add it to the respective dictionary.

Here's an example code that demonstrates this:

main.py
vowels = ['a', 'e', 'i', 'o', 'u']
# creating empty dictionaries to hold vowels and consonants
vowel_dict = {}
consonant_dict = {}

# iterating over each letter of alphabet and checking if it is vowel or consonant
for letter in 'abcdefghijklmnopqrstuvwxyz':
    if letter in vowels:
        vowel_dict[letter] = True
    else:
        consonant_dict[letter] = True

print("Vowels:", vowel_dict)
print("Consonants:", consonant_dict)
432 chars
15 lines

The output of the above code will be:

main.py
Vowels: {'a': True, 'e': True, 'i': True, 'o': True, 'u': True}
Consonants: {'b': True, 'c': True, 'd': True, 'f': True, 'g': True, 'h': True, 'j': True, 'k': True, 'l': True, 'm': True, 'n': True, 'p': True, 'q': True, 'r': True, 's': True, 't': True, 'v': True, 'w': True, 'x': True, 'y': True, 'z': True}
308 chars
3 lines

In the above code, we first initialize a list of vowels and then create empty dictionaries to hold vowels and consonants. We then iterate over each letter of the alphabet and check if the letter is a vowel or consonant, and if it is a vowel, we add it to vowel_dict, otherwise, we add it to consonant_dict. Finally, we print both the dictionaries.

related categories

gistlibby LogSnag