Table of Contents
What is a return list in Python and why do you need one
When a function returns a list, it returns an iterable. An iterable is an object that can be iterated over to generate new values. A generator is similar to an iterator, but the operation “next” won’t stop on the first element like it does with a list. The “next” operation returns the next element in a loop and then pauses until it is called again. As an example, if you have a generator that yields a value 10 and then pauses, the next call to the “next” operation will return the next value, which is 20.
The “iter” function returns an iterator over a container. You can use it to loop over all of the items in a list:
>>> my_list = [‘one’, ‘two’, ‘three’]
>>> my_iter = iter(my_list) # Returns an iterator over the list.
>>> while True: # Loop forever.
… print(my_iter.next()) # Returns the next element.
…
one
two
three
This loop will continue as long as you keep calling the next iteration, moving from one item to the next in a list. You can also use a generator function to do this without an iterator:
>>> def my_generator(): # A generator function. Yields values. But pauses when it is done.
… yield 10
… yield 20
… yield 30
… yield 40

How to create a return list in Python
Python’s return statement doesn’t return a simple object such as a string or a list. Instead it returns an iterable, which is either a generator function or an object with the __iter__ method. If you want to return a list, use this code:
my_list = [1, 2, 3]
return my_list
This will return an empty list.
To create a list as output in Python, do this:
list1 = [1, 2, 3]
print(list1) () # or list2 = [] # list2.append(1) (list2) # or list3 = [] # list3.append(1) # list4 = list3 + [2, 3] #
This code will return a list containing the numbers 1 to 3. You can also do this:
list1 = [1, 2, 3]

Examples of how to use a return list in Python
The function create_list takes any number of arguments and returns a list with the values, concatenated in order.
You can use the return list in python to create a list of values and then append new items to that list.
Here is an example:
The function squares takes any numeric argument and returns the square of that number as a string.
Here is an example:
The function hello takes no arguments and prints “Hello” to standard output.
Here is an example:
The function hello takes no arguments and prints “Hello” to standard output.
Here is an example:
The function square takes any numeric argument and returns the square of that number as a string.
Here is an example:
The function hello takes no arguments and prints “Hello” to standard output.
Here is an example:
The function format_time takes a dict parameter “key1 value1 key2 value2 … keyN valueN” and returns a string with the time in HH:MM:SS format.
Here is an example:
The function hello takes no arguments and prints “Hello” to standard output.
Here is an example:

Tips for creating an effective return list in Python
Unlike the examples with functions that return lists, don’t let the function return a list. Instead, have your function return an object that has a standard “list” behavior. For example:
blogger_posts = Blog.objects.filter(is_published=False).order_by(‘-pubdate’)
This can prevent unexpected results and make your function much easier to maintain.
The “python return list” blog post is about tips for creating an effective return list in Python. The post is intended to help beginners in learning the basics of programming avoid common errors when writing a program with this particular kind of data structure called a “list”. It’s not intended to teach programming or how to create the list itself.
If you have any questions, please leave a comment below and I’ll help you out. And, if you found this helpful, please share it with your friends. Thanks!
Posted on Dec 10th 2013 by Nathan [ARTICLE END]
Please enable JavaScript to view the comments powered by Disqus.
Disqus: http://en.gravatar.com/[email protected]
