Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

How can I loop trough an list and display only the words in the list that are beginning with the letter "A" for example.

I can't figure this out. Someone please help me.

2 Answers

Brandon Oakes
seal-mask
.a{fill-rule:evenodd;}techdegree
Brandon Oakes
Python Web Development Techdegree Student 11,501 Points

yourlist = [adkfjkADFADSF]

    for let in yourlist:
        if let.lower() == 'a':
             print(let) 

One way as shown above is to first use a for loop to iterate through each item in the list, then within the for loop you can use an if statement to say if the current item is == 'a' then display it. The .lower() is just to lowercase the item in your list, I am using it assuming that your list has no numbers or special characters in it.

John Lack-Wilson
John Lack-Wilson
8,181 Points

If we have a list of strings, i.e. something like:

lst_of_words = ["hello","world"]

Then we can use a for loop like below:

for word in lst_of_words:
    if word[0].lower() is "a":
        print(word)