Return List from Function - Python Last Updated : 13 May, 2025 Comments Improve Suggest changes Like Article Like Report In Python, functions can return a wide variety of data types and we can return list too. Our task is to return a list from a Python function. We'll do this using different techniques—returning static data, computed values and generator-based lists, through short, clear examples.We can simply construct the list within function and return it. This method is both concise and readable. Example: Python def get_list(): li = [1, 2, 3, 4, 5] return li res = get_list() print(res) Output[1, 2, 3, 4, 5] Exaplanation: The function get_list() creates a list li with the elements [1, 2, 3, 4, 5] and returns it to the caller. When the function is called, the returned list is assigned to the variable res, which is then printed.Examples of Returning a List from a FunctionExample 1: Return a Static ListThis function returns a predefined list of fruit names, demonstrating how to return a constant list without computation. Python def get_fruits(): return ["apple", "banana", "cherry"] f = get_fruits() print(f) Output['apple', 'banana', 'cherry'] Explanation: The list is created inside the function and returned as-is when the function is called.Example 2: Return a Computed ListThis function calculates the square of each number from 0 to n-1 and returns them in a list. Python def sq(n): r = [] for i in range(n): r.append(i * i) return r print(sq(5)) Output[0, 1, 4, 9, 16] Explanation: It iterates from 0 to n-1, computes i*i for each value, and collects the results in the list r.Example 3: Using List Constructor with a GeneratorA more advanced approach involves using a generator expression within the list constructor to return a list. Python def generate_list(n): return list(i for i in range(n) if i % 2 == 0) res = generate_list(10) print(res) Output[0, 2, 4, 6, 8] Explanation:The code uses list constructor to create a list of even number from 0 to 9....It then return the list and we print them by calling the function.Related Articles:Python ListsPython FunctionsGenerators in Python Comment More infoAdvertise with us Next Article Return List from Function - Python S shivangtomar257 Follow Improve Article Tags : Python Python Programs Python-Functions Practice Tags : pythonpython-functions Similar Reads How to return null in Python ? In Python, we don't have a keyword called null. Instead, Python uses None to represent the absence of a value or a null value. We can simply return None when you want to indicate that a function does not return any value or to represent a null-like state.Pythondef my_function(): return None # Call t 1 min read SyntaxError: âreturnâ outside function in Python We are given a problem of how to solve the 'Return Outside Function' Error in Python. So in this article, we will explore the 'Return Outside Function' error in Python. We will first understand what this error means and why it occurs. Then, we will go through various methods to resolve it with examp 4 min read Can I call a function in Python from a print statement? Calling a function from a print statement is quite an easy task in Python Programming. It can be done when there is a simple function call, which also reduces the lines of code. In this article, we will learn how we can call a function from a print statement.Calling a Function Inside print()In this 1 min read Map Reduce and Filter Operations in Python In this article, we will study Map, Reduce, and Filter Operations in Python. These three operations are paradigms of functional programming. They allow one to write simpler, shorter code without needing to bother about intricacies like loops and branching. In this article, we will see Map Reduce and 3 min read Accessing Python Function Variable Outside the Function In Python, function variables have local scope and cannot be accessed directly from outside. However, their values can still be retrieved indirectly. For example, if a function defines var = 42, it remains inaccessible externally unless retrieved indirectly.Returning the VariableThe most efficient w 4 min read Python String Input Output In Python, input and output operations are fundamental for interacting with users and displaying results. The input() function is used to gather input from the user and the print() function is used to display output.Input operations in PythonPythonâs input() function allows us to get data from the u 3 min read Interesting Facts About Python Python is a high-level, general-purpose programming language that is widely used for web development, data analysis, artificial intelligence and more. It was created by Guido van Rossum and first released in 1991. Python's primary focus is on simplicity and readability, making it one of the most acc 7 min read Output of Python Program | Set 1 Predict the output of following python programs: Program 1:Python r = lambda q: q * 2 s = lambda q: q * 3 x = 2 x = r(x) x = s(x) x = r(x) print (x) Output:24Explanation : In the above program r and s are lambda functions or anonymous functions and q is the argument to both of the functions. In firs 2 min read Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio 10 min read sympy.transforms.inverse_mellin_transform() in python With the help of inverse_mellin_transform method, we can compute the inverse mellin transform and return the function. Syntax : inverse_mellin_transform(F, s, x, strip) Return : Return the function F(x). Example #1 : In this example we can see that by using inverse_mellin_transform() method, we are 1 min read Like