0

Hi guys, I am trying to iterate through a list.
This is my code:

#assume this is the list:
list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g']


def iter_list(a):
    for s in a:
        return s

#here it is when it runs:
print iter_list(list1)
a

why doesn't it iterate over the entire list? it stops in the first character.

simple enough right?
When I run it, I just get the first character of the list.
what happened?

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by HoneyBadger
1

Add a print statement to see what is going on.

#assume this is the list:
list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g']


def iter_list(a):
    for s in a:
        print "testing", s
        return s

#here it is when it runs:
print iter_list(list1)
a

Edited by zentraders: n/a

0

ahh yes. So why does print work, and return doesn't?
I thought they were basically the same thing.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.