Generate a list of strings with their first letters removed


This Python code takes a list of strings and creates a new list called without_first_letters using a list comprehension. The new list contains the same words as the original list, but with their first letters removed. Here's how the code works:

  • strings = ["apple", "banana", "cherry"]: This line initializes a variable named strings and assigns it a list containing three words.
  • without_first_letters = [word[1:] for word in strings]: This line of code initializes a variable named without_first_letters and assigns it the result of a list comprehension.
    • for word in strings: This part sets up a loop that iterates through each word word in the strings list.
    • word[1:]: For each word in the list, this expression slices the word starting from the second character (index 1) and includes all characters after the first one.
    • [word[1:] for word in strings]: This is the list comprehension itself. It iterates through the words in the strings list, removes the first letter from each word, and includes the modified words in the new list.
  • print(strings): This line of code prints the original strings list to the console.
  • print(without_first_letters): This line of code prints the without_first_letters list to the console.

Source Code

strings = ["apple", "banana", "cherry"]
without_first_letters = [word[1:] for word in strings]
print(strings)
print(without_first_letters)

Output

['apple', 'banana', 'cherry']
['pple', 'anana', 'herry']

Example Programs