I need help - Lists and Python

How to return a list in Python ???

When I tried to return a list, I got an empty list. What reason???

-3


source to share


2 answers


As Andrey pointed out, you will get better answers if you show us the code you are currently using. Also if you could indicate which version of Python you are using, that would be great.

There are several ways to get the list back. Let's say for example we have a function called retlist.

def retlist():
    return []

      

will return an empty list

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

      

will return [5].



You can also use a list comprehension to return a list

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

      

There are many ways to return a list. But mostly it has to do with returns.

If you are after a more detailed answer, please comment what you need.

Luck

+2


source


to:



In [1]: def pants():
   ...:     return [1, 2, 'steve']
   ...: 
In [2]: pants()
Out[2]: [1, 2, 'steve']

      

0


source







All Articles