[Tutor] List Python Question..Please help

Steven D'Aprano steve at pearwood.info
Sat Sep 28 17:00:54 CEST 2013


On Sat, Sep 28, 2013 at 12:36:13AM -0500, Jacqueline Canales wrote:
> Thank you guys so much i was able to figure it out. I definitely thought to
> much into the the problem and made it harder on myself. Cant thank you
> enough for assisting me. I have one more problem with the coding tho.
> 
> composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
> new_list = []
> person = new_list

This line above (person = new_list) is unnecessary, since it never gets 
used before the next line sets it to something else.

> for person in composers:
>     if person[0].lower() == person[-1].lower():
>         print(person)
> 
> Output:
> Saint-Saens
> Easdale
> Nielsen
> 
> composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
> new_list = []
> person = new_list
> for person in composers:
>     if person[0].lower() == person[-1].lower():
>         new_list.append(person)
>         print(new_list)

Here you print the content of new_list each time through the loop, so 
you see it growing. Instead, get rid of the indentation so that it runs 
when the loop is finished:

for person in composers:
    if person[0].lower() == person[-1].lower():
        new_list.append(person)

print(new_list)


-- 
Steven


More information about the Tutor mailing list