'Return' keyword returns only one element from a loop?

后端 未结 3 632
死守一世寂寞
死守一世寂寞 2020-12-04 02:37

I have a simple function to read the csv file and extracts the first coloum from it:

import csv 

def pass_username():
    with open(\'test.csv\', \'r\') as          


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-04 02:41

    When you call return, it autimtically exits out of the function, so it doesn't process anything after it reaches the return statement.

    You might want to append the result to a list, then return the list.

    In your case, you might even want to use a list comprehension, so instead of the for loop, use this:

    return [row[0] for row in spamreader]
    

提交回复
热议问题