Returning every element from a list (Python)
Question:
I know that it is possible for a function to return multiple values in Python. What I would like to do is return each element in a list as a separate return value. This could be an arbitrary number of elements, depending on user input. I am wondering if there is a pythonic way of doing so?
For example, I have a function that will return a pair of items as an array, e.g., it will return [a, b]
.
However, depending on the input given, the function may produce multiple pairs, which will result in the function returning [[a, b], [c, d], [e, f]]
. Instead, I would like it to return [a, b], [c, d], [e, f]
As of now, I have implemented a very shoddy function with lots of temporary variables and counts, and am looking for a cleaner suggestion.
Appreciate the help!
Answers:
There is a yield statement which matches perfectly for this usecase.
def foo(a):
for b in a:
yield b
This will return a generator which you can iterate.
print [b for b in foo([[a, b], [c, d], [e, f]])
Check out this question:
How to return multiple values from *args?
The important idea is return values, as long as they’re a container, can be expanded into individual variables.
However, depending on the input given, the function may produce multiple pairs, which will result in the function returning [[a, b], [c, d], [e, f]]. Instead, I would like it to return [a, b], [c, d], [e, f]
Can you not just use the returned list (i.e. the list [[a, b], [c, d], [e, f]]) and extract the elements from it? Seems to meet your criteria of arbitrary number of / multiple values.
When a python function executes:
return a, b, c
what it actually returns is the tuple (a, b, c)
, and tuples are unpacked on assignment, so you can say:
x, y, z = f()
and all is well. So if you have a list
mylist = [4, "g", [1, 7], 9]
Your function can simply:
return tuple(mylist)
and behave like you expect:
num1, str1, lst1, num2 = f()
will do the assignments as you expect.
If what you really want is for a function to return an indeterminate number of things as a sequence that you can iterate over, then you’ll want to make it a generator using yield
, but that’s a different ball of wax.
The answer is very simple just join the elements of list by providing a required separator
Syntax for join method :
"separator".join(iterable)
For example let us consider a function where you need to return all the list values separated by a comma (,)
def fun(samp_list):
return ",".join(samp_list)
print(fun(["1","2","3"]))
And the answer as follows
1,2,3
Example 2 : Return list values separated by a new line
def fun(samp_list):
return "/n".join(samp_list)
print(fun(["1","2","3"]))
And the output for this function is as follows
1
2
3
Hope it’s useful
Do support by upvoting !
return *(x for x in range(10)),
This way you can make use of list comprehension-type syntax that you are used to, however you are not creating a list. Instead, you are unpacking (notice *
) a generator expression (notice ()
instead of []
) into a tuple (notice ,
), which you return.
Some more information: https://stackoverflow.com/a/47476344/9903597
I know that it is possible for a function to return multiple values in Python. What I would like to do is return each element in a list as a separate return value. This could be an arbitrary number of elements, depending on user input. I am wondering if there is a pythonic way of doing so?
For example, I have a function that will return a pair of items as an array, e.g., it will return [a, b]
.
However, depending on the input given, the function may produce multiple pairs, which will result in the function returning [[a, b], [c, d], [e, f]]
. Instead, I would like it to return [a, b], [c, d], [e, f]
As of now, I have implemented a very shoddy function with lots of temporary variables and counts, and am looking for a cleaner suggestion.
Appreciate the help!
There is a yield statement which matches perfectly for this usecase.
def foo(a):
for b in a:
yield b
This will return a generator which you can iterate.
print [b for b in foo([[a, b], [c, d], [e, f]])
Check out this question:
How to return multiple values from *args?
The important idea is return values, as long as they’re a container, can be expanded into individual variables.
However, depending on the input given, the function may produce multiple pairs, which will result in the function returning [[a, b], [c, d], [e, f]]. Instead, I would like it to return [a, b], [c, d], [e, f]
Can you not just use the returned list (i.e. the list [[a, b], [c, d], [e, f]]) and extract the elements from it? Seems to meet your criteria of arbitrary number of / multiple values.
When a python function executes:
return a, b, c
what it actually returns is the tuple (a, b, c)
, and tuples are unpacked on assignment, so you can say:
x, y, z = f()
and all is well. So if you have a list
mylist = [4, "g", [1, 7], 9]
Your function can simply:
return tuple(mylist)
and behave like you expect:
num1, str1, lst1, num2 = f()
will do the assignments as you expect.
If what you really want is for a function to return an indeterminate number of things as a sequence that you can iterate over, then you’ll want to make it a generator using yield
, but that’s a different ball of wax.
The answer is very simple just join the elements of list by providing a required separator
Syntax for join method :
"separator".join(iterable)
For example let us consider a function where you need to return all the list values separated by a comma (,)
def fun(samp_list):
return ",".join(samp_list)
print(fun(["1","2","3"]))
And the answer as follows
1,2,3
Example 2 : Return list values separated by a new line
def fun(samp_list):
return "/n".join(samp_list)
print(fun(["1","2","3"]))
And the output for this function is as follows
1
2
3
Hope it’s useful
Do support by upvoting !
return *(x for x in range(10)),
This way you can make use of list comprehension-type syntax that you are used to, however you are not creating a list. Instead, you are unpacking (notice *
) a generator expression (notice ()
instead of []
) into a tuple (notice ,
), which you return.
Some more information: https://stackoverflow.com/a/47476344/9903597