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

Christian Gollwitzer auriocus at gmx.de
Tue Jun 15 02:13:29 EDT 2021


Am 12.06.21 um 04:02 schrieb Jach Feng:
>>>> def foo():
> ...     # do something
> ...
>>>> a = []
>>>> for i in range(3):
> ...     a.append(foo())
> ...
>>>> a
> []
>>>>

How about having "foo" return a list of things? Then you can append that 
list and return an empty list if you want nothing added:

In [1]: def foo():
    ...:     return [1,2,3]
    ...:

In [2]: def bar():
    ...:     return []
    ...:

In [3]: a=[]

In [4]: a += foo()

In [5]: a
Out[5]: [1, 2, 3]

In [6]: a += bar()

In [7]: a
Out[7]: [1, 2, 3]


	Christian


More information about the Python-list mailing list