-3

I'm having some unexpected results with trying to iterate through a list of lists using a function.

A = [[1,2,3],
     [4,5,6]]

def list_of_lists(l):
    for i in l:
        return i

print list_of_lists(A)

Out : [1, 2, 3]

A = [[1,2,3],
     [4,5,6]]

def list_of_lists(l):
    for i in l:
        print i

print list_of_lists(A)

Out : [1, 2, 3]
      [4, 5, 6]
      None

Why does it seem like I'm only returning the first element of A when my function uses return?

SELECTCOUNTSTAR
  • 100
  • 2
  • 11

1 Answers1

1

use yield instead of return if you want to "return" multiple values. return exits the function at the first execution. yield can be used to return multiple values. The returned values are treated like a list

Felk
  • 7,720
  • 2
  • 35
  • 65