Is there a way to get the following result in Python?

Jach Feng jfong at ms4.hinet.net
Tue Jun 15 03:33:51 EDT 2021


Peter Otten 在 2021年6月15日 星期二下午2:48:07 [UTC+8] 的信中寫道:
> On 12/06/2021 04:02, Jach Feng wrote: 
> 
> >>>> def foo(): 
> > ... # do something 
> > ... 
> >>>> a = [] 
> >>>> for i in range(3): 
> > ... a.append(foo()) 
> > ... 
> >>>> a 
> > []
> The most natural way to achieve something similar is to replace append() 
> with extend(): 
> 
> >>> def foo(): return ()
> >>> a = [] 
> >>> for i in range(3):
> a.extend(foo()) 
> 
> 
> >>> a 
> []
Yes, return a list and using extend() to collect value is exactly what I need!
Thank you, Christian and Peter.

--Jach


More information about the Python-list mailing list