Let's break down the second line of code in your Python program:
words = ['Emotan', 'Amina', 'Ibeno', 'Santwala']
new_list = [(word[0], word[-1]) for word in words if len(word) > 5]
print(new_list)
new_list = [(word[0], word[-1]) for word in words if len(word) > 5]
This is list comprehension, which creates a new list.
It iterates over each word
in the words
list.
The condition if len(word) > 5
ensures that only words with more than 5 characters are included.
(word[0], word[-1])
extracts the first (word[0]
) and last (word[-1]
) characters of each word.
'Emotan' → Length = 6 (greater than 5) → Include → ('E', 'n')
'Amina' → Length = 5 (not greater than 5) → Excluded
'Ibeno' → Length = 5 (not greater than 5) → Excluded
'Santwala' → Length = 8 (greater than 5) → Include → ('S', 'a')
[('E', 'n'), ('S', 'a')]
Would you like me to modify the code or add more explanations?