Python Forum
Function - Return multiple values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function - Return multiple values
#1
Greeting!
I have to scan directories and find/process subdirectories.
Number of SubDirs not static.
I thought I could use a function but found I cannot "return" all the SubDirs.
I can print all of them but not return them.
I'm sure it is simple but I could not find a clear answer but I cannot find one.
def func(dir1):
    for ed1 in os.listdir(dir1) :
        echd_p = os.path.join(dir1,ed1)
        if os.path.isdir(echd_p) :
            return (echd_p)

fdp =  func(dir1)
print(type(fdp))   
print(fdp)  
Thank you.
Reply
#2
This might be another way to go about solving the problem:

import os

def subdirectory_iterator (root_directory) :
	for root, dirs, files in os.walk(root_directory, topdown=False):
		for name in dirs:
			yield os.path.join(root, name)

for directory in subdirectory_iterator (dir1) :
	print (directory)
Reply
#3
Thanks! I could return all the values by replacing 'return' with 'print'
def func(dir1):
    for ed1 in os.listdir(dir1) :
        echd_p = os.path.join(dir1,ed1)
        if os.path.isdir(echd_p) :
            print (echd_p)
What I'm interested in is how I can 'return' multiple values.
I have no idea how many Subdirs I'll get...
Reply
#4
Just found I can make a list and pass it from a function....
Not sure if I tis good way to do it but it works.
def func(dir1):
    dir_l=[]
    for ed1 in os.listdir(dir1) :
        echd_p = os.path.join(dir1,ed1)
        if os.path.isdir(echd_p) :
            #print (echd_p)
            dir_l.append(echd_p)
    return(dir_l)

fdp =  func(dir1)
print(type(fdp))   
print(fdp)
Thank you!
Have a great rest of the day! Smile
Reply
#5
Or a tuple
def func(a):
    return (a, a*a, a**3, a**4, a**5)

a, a2, a3, a4, a5 = func(5)

print(a, a2, a3, a4, a5)
Output:
5 25 125 625 3125
Or as a list using this form:
def func(a):
    return a, a*a, a**3, a**4, a**5  # <- Look Ma, no brackets!

a, a2, a3, a4, a5 = func(5)

print(a, a2, a3, a4, a5)
And if it makes sense to have a sequence you can use a generator and get the values one at a time.
def func(a, power):
    b = a
    for _ in range(power):
        yield b
        b *= a

print(list(func(5, 5)))
Output:
[5, 25, 125, 625, 3125]
Which way is "best" depends on your needs and tastes. In general I avoid functions that return multiple values, but sometimes it is the best solution.
tester_V likes this post
Reply
#6
Well, I do not know what that is but it looks impressive! Wink

Thank you!
Reply
#7
(Jun-01-2021, 01:11 AM)tester_V Wrote: Thanks! I could return all the values by replacing 'return' with 'print'

No, you can't! All functions which don't have return or yield return None. Printing is not same as returning

>>> def my_func():
...     print('Hello world')
...
>>> print(my_func())
Hello world                    # what your function does
None                           # what your function returns
>>>
If you print from function then you give away flow control:

>>> value = my_func()         # I want to bind name to value returned by function
Hello world                   # as 'side-effect' it prints; there is no way to control it
>>> print(value)              # return value None is binded to name
None
tester_V likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
This is your code written as a generator.
def func(dir1):
    """Generator that returns directories in dir1"""
    for ed1 in os.listdir(dir1) :
        echd_p = os.path.join(dir1, ed1)
        if os.path.isdir(echd_p):
            yield echd_p
 
for fdp in func(dir1):
    print(fdp)
In this case I think a generator works better than a returning a list.
tester_V likes this post
Reply
#9
Guys, I really appreciate your help!
I did not even use a function before.
Reply
#10
Generators are very useful. Python is switching over to using them all over the place. It is time you learn how to use them.
tester_V likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  __init__() got multiple values for argument 'schema' dawid294 4 2,352 Jan-03-2024, 09:42 AM
Last Post: buran
  Need to return 2 values from 1 DF that equals another DF cubangt 5 643 Oct-21-2023, 02:45 PM
Last Post: deanhystad
  nested function return MHGhonaim 2 613 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,284 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 1,183 Feb-04-2023, 12:30 PM
Last Post: caslor
  Adding values with reduce() function from the list of tuples kinimod 10 2,666 Jan-24-2023, 08:22 AM
Last Post: perfringo
  [Solved]Return values from npyscreen Extra 2 1,160 Oct-09-2022, 07:19 PM
Last Post: Extra
  Parallelism with return values Plexian 7 1,498 Aug-14-2022, 09:33 AM
Last Post: Plexian
  How to combine multiple column values into 1? cubangt 15 2,839 Aug-11-2022, 08:25 PM
Last Post: cubangt
  function accepts infinite parameters and returns a graph with those values edencthompson 0 863 Jun-10-2022, 03:42 PM
Last Post: edencthompson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020