gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
I need a python code to print out the list of all unique first letters of all the words…
- User
I need a python code to print out the list of all unique first letters of all the words in a list
- Assistant
Here's a Python code that prints out the list of all unique first letters of all the words in a given list:
word_list = ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig', 'grape'] first_letters = [] for word in word_list: letter = word[0].lower() if letter not in first_letters: first_letters.append(letter) print(first_letters)Output:
['a', 'b', 'c', 'd', 'e', 'f', 'g']In this code, we iterate over each word in the
word_list. We extract the first letter of each word using indexingword[0]and convert it to lowercase using.lower()to ensure consistency. If the letter is not already present in thefirst_letterslist, we append it to the list. Finally, we print thefirst_letterslist containing all the unique first letters.