Final answer:
This detailed answer explains how to implement a Python program that counts word frequencies in a list of words while ensuring case insensitivity and displaying the output in alphabetical order.
Step-by-step explanation:
Python Program to Count Word Frequencies
To implement the program, follow the steps below:
- Create an empty dictionary to store the word frequencies.
- Read the list of words from the user.
- Iterate over each word in the list and convert it to lowercase using the lower() function.
- If the word is already in the dictionary, increment its frequency by 1. Otherwise, add it to the dictionary with a frequency of 1.
- Sort the dictionary by keys using the sorted() function.
- Display each word and its frequency from the sorted dictionary.
Example:
word_list = ['Apple', 'banana', 'Orange', 'apple', 'banana']
word_frequencies = {}
for word in word_list:
word = word.lower()
if word in word_frequencies:
word_frequencies[word] += 1
else:
word_frequencies[word] = 1
sorted_word_frequencies = sorted(word_frequencies.items())
for word, frequency in sorted_word_frequencies:
print(word, frequency)