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?

Recommended Answers

All 2 Replies

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.