views:

176

answers:

2

How to return a list in Python???

When I tried returning a list,I got an empty list.What's the reason???

A: 

to wit:

In [1]: def pants():
   ...:     return [1, 2, 'steve']
   ...: 
In [2]: pants()
Out[2]: [1, 2, 'steve']
Autoplectic
What IDE produced that log?
S.Lott
It's IPython - http://ipython.scipy.org/moin/
Federico Ramponi
And very worth a try! Wouldn't go anywhere without it :)
Ole
+2  A: 

As Andrew commented, you will receive better answers if you show us the code you are currently using. Also if you could state what version of Python you are using that would be great.

There are a few ways you can return a list. Say for example we have a function called retlist.

def retlist():
    return []

will return the empty list

def retlist():
    a = list()
    a.append(5)
    return a

will return [5].

You can also use list comprehension to return a list

def retlist():
    return [x*x for x in range(10)]

There are plenty of ways to return a list. But basically it involves return .

If you are after a more detailed response, comment for what you need.

Good Luck

mdec