Program: Write a program to print all the words starting with an alphabet “a” and also print the location at which the word is present in the list.
In this program of Python you would be introduced to usage of lists and iterating through them, lists are one of the important topics in python and is widely used for many purposes in writing hug programs.
We had picked up this problem from one of the facebook groups who were looking for a solution 🙂
#below variable maintains the index while we are iterating through a list index = 0; #Initialise a list words as below items=["apple", "aardvark", "parrot", "table", "noise", "annoying", "conversation", "abrupt", "obsolete", "herring", "ant", "helicopter"] ; #iterate through each word in a list and check for words starting with alphabet "a" for item in items: if item[0] == 'a': print item , " at location:" , index; index = index + 1; #increment the word count irrespective of whether it starts with "a" or not.
The above code will print the list of all words which start with alphabet “a” along with the location at which it is present in the list of words.
Please do revert to us in form of comments, we would be more than happy to respond.