[Tutor] can i pass a list to a function and get one back ?

Kent Johnson kent37 at tds.net
Sun Oct 22 13:33:54 CEST 2006


Danny Yoo wrote:
> One small comment: we often don't need to use temporary variables like 
> 'var'.
> 
>     def return_a_list(some_var):
>         some_list = []
>         for i in range(5):
>             some_list.append(some_var + i)
>         return some_list
> 
> Sometimes a temporary variable is useful, and sometimes not.  Here, it 
> seems like it's not too necessary.

some_list is not too necessary either; this is a good place for a list 
comprehension:
def return_a_list(some_var):
   return [ some_var + i for i in range(5) ]

or for that matter
   return range(some_var:some_var+5)

Kent



More information about the Tutor mailing list